您好,欢迎访问三七文档
R语言作业1-5章第二章1.矩阵A:1.000.800.260.670.340.801.000.330.590.340.260.331.000.370.210.670.590.371.000.350.340.340.210.351.00矩阵A的逆矩阵[,1][,2][,3][,4][,5][1,]3.3881372-2.12222330.23706087-1.0684729-0.10622799[2,]-2.12222332.9421167-0.33593309-0.1330915-0.16163579[3,]0.2370609-0.33593311.20698521-0.3763728-0.08811984[4,]-1.0684729-0.1330915-0.376372842.0091273-0.21562437[5,]-0.1062280-0.1616358-0.08811984-0.21562441.18504738特征值[1]2.79225690.82633660.77906380.42058730.1817554x1=c(1.00,0.80,0.26,0.67,0.34,0.80,1.00,0.33,0.59,0.34,0.26,0.33,1.00,0.37,0.21,0.67,0.59,0.37,1.00,0.35,0.34,0.34,0.21,0.35,1.00);x1A=matrix(x1,nrow=5,ncol=5);Asolve(A)A.e=eigen(A);A.eA.e$vectors%*%diag(A.e$values)%*%t(A.e$vectors)特征向量:[,1][,2][,3][,4][,5][1,]-0.52554260.3402197-0.16650860.159377710.74493565[2,]-0.51867160.2343491-0.17777390.50822995-0.62141694[3,]-0.3131429-0.9030775-0.22870380.149427890.10843643[4,]-0.49664330.0386900-0.1185744-0.83115510-0.21672526[5,]-0.3317705-0.11083870.93504330.056156550.013548312.2平均工资1897.2工资方差455.9808工资中位数1872.5各组频率0.120.140.260.220.140.080.04累计频率0.120.260.520.740.880.961.00近似服从正态分布e2.2=read.table(clipboard,header=T);e2.2attach(e2.2)mean(X)sd(X)median(X)f=hist(X,breaks=c(1000,1300,1600,1900,2200,2500,2800,3100))f$countf$count/length(X)cumsum(f$count/length(X))qqnorm(X)qqline(X)detach(e2.2)-2-101210001500200025003000NormalQ-QPlotTheoreticalQuantilesSampleQuantilesHistogramofXXFrequency100015002000250030000246810122.3按列统计:e2.3=read.table(clipboard,header=T)e2.3summary(e2.3)attach(e2.3)barplot(table(Smoke))plot(Study,Smoke)table(Smoke,Study)tab=table(Smoke,Study)prop.table(tab,1)prop.table(tab,2)prop.table(tab)prop=function(x)x/sum(x)apply(tab,2,prop)t(apply(tab,1,prop))StudySmoke5h10h5-10hN0.00.20.2Y0.30.10.2StudySmoke5h10h5-10hN0.00000000.50000000.5000000Y0.50000000.16666670.3333333Study5h10h5-10hN00.66666670.5Y10.33333330.5xy5h10h5-10hNY0.00.20.40.60.81.0编号是否抽烟Smoke每天学习时间StudyMin.:1.00否:4N:45-10小时:45h:31stQu.:3.25是:6Y:6超过10小时:310h:3Median:5.50少于5小时:35-10h:4Mean:5.503rdQu.:7.75Max.:10.00第三章3.2barplot(apply(e3.2,1,mean))boxplot(e3.2)e3.2=read.table(clipboard,header=T)e3.2summary(e3.2)barplot(apply(e3.2,1,mean))boxplot(e3.2)stars(e3.2,full=T)stars(e3.2,full=T,draw.segments=T)library(aplpack)faces(e3.2,ncol.plot=7)library(mvstats)plot.andrews(e3.2)广州市珠海市江门市肇庆市汕尾市清远市潮州市050010001500summary(e3.2)x1x2x3x4Min.:2.41Min.:0.68Min.:2.17Min.:0.011stQu.:26.211stQu.:7.561stQu.:25.051stQu.:0.64Median:70.06Median:16.90Median:65.64Median:1.94Mean:407.06Mean:104.75Mean:390.92Mean:28.913rdQu.:465.073rdQu.:77.253rdQu.:447.083rdQu.:31.75Max.:3266.52Max.:940.86Max.:3120.18Max.:350.60stars(e3.2,full=T)x1x2x3x4050010001500200025003000广州市韶关市深圳市珠海市汕头市佛山市江门市湛江市茂名市肇庆市惠州市梅州市汕尾市河源市阳江市清远市东莞市中山市潮州市揭阳市云浮市stars(e3.2,full=T,draw.segments=T)plot.andrews(e3.2)广州市韶关市深圳市珠海市汕头市佛山市江门市湛江市茂名市肇庆市惠州市梅州市汕尾市河源市阳江市清远市东莞市中山市潮州市揭阳市云浮市-3-2-10123-10000100020003000400050006000广州市韶关市深圳市珠海市汕头市佛山市江门市湛江市茂名市肇庆市惠州市梅州市汕尾市河源市阳江市清远市东莞市中山市潮州市揭阳市云浮市第四章4.1回归方程为Y=-0.6667+1.2667x残差平方和101.4667残差为3.0000003.333333-5.2000001.600000-6.2666673.5333334.2Call:lm(formula=y~x1+x2)Coefficients:(Intercept)x1x25.319e-178.152e-01-2.123e-02x=c(10,5,7,19,11,8)y=c(15,9,3,25,7,13)xyplot(x,y)lxy-function(x,y){n=length(x);sum(x*y)-sum(x)*sum(y)/n}lxy(x,x)lxy(x,y)b1=lxy(x,y)/lxy(x,x)b0=mean(y)-b1*mean(x)c(b1=b1,b0=b0)fm=lm(y~x)fmy1=b0+b1*xy1e=y-y1ee2=sum(e^2)e2x1=c(10,5,7,19,11,8)x2=c(2,3,3,6,7,9)y=c(15,9,3,25,7,13)y=(y-mean(y))/sd(y)x1=(x1-mean(x1))/sd(x1)x2=(x2-mean(x2))/sd(x2)fm=lm(y~x1+x2)fm4.3相关系数为0.9489428残差的方差为0.2048199回归方程为Y=0.1181+0.0036xR2=0.9489428方差分析AnalysisofVarianceTableResponse:yDfSumSqMeanSqFvaluePr(F)x116.681616.681672.3962.795e-05***Residuals81.84340.2304---Signif.codes:0‘***’0.001‘**’0.01‘*’0.05‘.’0.1‘’1X等于1000时预测y=3.703262x=c(825,215,1070,550,480,920,1350,325,670,1215)y=c(3.5,1,4,2,1,3,4.5,1.5,3,5)plot(x,y)cor(x,y)lxy-function(x,y){n=length(x);sum(x*y)-sum(x)*sum(y)/n}lxy(x,x)lxy(x,y)b=lxy(x,y)/lxy(x,x)a=mean(y)-b*mean(x)y1=a+b*xe=y-y1(sd(e))^2fm=lm(y~x)fm(R2=summary(fm)$r.sq)(R=sqrt(R2))anova(fm)y0=a+b*1000y02004006008001000120012345xy4.4cor(e4.4)x1x2x3x11.00000000.31981160.5796714x20.31981161.00000000.4816453x30.57967140.48164531.0000000Call:lm(formula=x3~x1+x2,data=e4.4)Coefficients:(Intercept)x1x2-22.74500.15111.2166Call:lm(formula=x3~x1+x2,data=e4.4)Residuals:12345678-1.1335-3.9430-0.651715.6699-0.59392.7178-16.27454.2089Coefficients:EstimateStd.ErrortvaluePr(|t|)(Intercept)-22.745030.6939-0.7410.492x10.15110.11321.3350.239x21.21661.30940.9290.395Residualstandarderror:10.52on5degreesoffreedomMultipleR-squared:0.4338,AdjustedR-squared:0.2073F-statistic:1.915on2and5DF,p-value:0.2412复相关系数R=0.6586解释变量与依赖变量之间线性关系不明显。e4.4=read.table(clipboard,header=T);e4.4cor(e4.4)fm=lm(x3~x1+x2,data=e4.4)fmsummary(fm)R=sqrt(summary(fm)$r.squared);R4.5Call:lm(formula=y~x1+x2,data=e4.5)Coefficients:(Intercept)x1x2-5213.18508.8181.6回归方程为y=-5213.1+8508.8*x1+181.6*x2Call:lm(formula=y~x1+x2,data=e4.5)Residuals:123456781617.4206.91282.3-703.6-2215.0-770.3-310.6892.9Coefficien
本文标题:R语言作业
链接地址:https://www.777doc.com/doc-3287110 .html