收藏 分销(赏)

R软件期末考试复习提纲.doc

上传人:二*** 文档编号:4401772 上传时间:2024-09-18 格式:DOC 页数:10 大小:73KB
下载 相关 举报
R软件期末考试复习提纲.doc_第1页
第1页 / 共10页
亲,该文档总共10页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、#期末测验专项温习#一、矩阵与数据框#1.天生特定的矩阵与数据框#矩阵#办法一a=array(1:10,dim=c(2,5)rownames(a)=1:2colnames(a)=c(one,two,three,four,five)adimnames(a)=list(1:2,c(one,two,three,four,five)nrow=nrow(a)ncol=ncol(a)dim(a)#办法二a=matrix(1:10,nrow=2,byrow=F)rownames(a)=1:2colnames(a)=c(one,two,three,four,five)a=matrix(1:10,nrow=2,

2、byrow=F,dimnames=list(1:2,c(one,two,three,four,five)#数据框的天生df=data.frame(Name=c(Alice,Becka,James,Jeffrey,John),Sex=c(F,F,M,M,M),Age=c(13,13,12,13,12),Height=c(56.5,65.3,57.3,62.5,59.0),Weight=c(84.0,98.0,83.0,84.0,99.5);dfLst=list(Name=c(Alice,Becka,James,Jeffrey,John),Sex=c(F,F,M,M,M),Age=c(13,13,

3、12,13,12),Height=c(56.5,65.3,57.3,62.5,59.0),Weight=c(84.0,98.0,83.0,84.0,99.5)LstLstNameLstNameLst1Lst1Lst$Namedf=as.data.frame(Lst)dfx=array(1:6,dim=c(2,3)as.data.frame(x)#数据框的援用df1:2,3:5dfHeightdf$Weightnames(df)#此属性必定非空rownames(df)=c(one,two,three,four,five)dfattach(df)r=Height/Weightrdf$r=rname

4、s(df)detach()r=Height/Weight#2.矩阵的运算a=diag(1:3)a21=1a#1转置运算t(a)#2行列式det(a)#3向量内积x=1:5y=2*1:5x%*%yt(x)%*%ycrossprod(x,y)#4向量的外积x%*%t(y)tcrossprod(x,y)outer(x,y)x%o%y#矩阵的乘法a=array(1:9,dim=c(3,3)b=array(9:1,dim=c(3,3)x=1:3a*ba%*%bx%*%a%*%xcrossprod(a,b)#t(a)%*%btcrossprod(a,b)#a%*%t(b)#矩阵的逆solve(a)b=1:

5、3solve(a,b)#ax=b的解#矩阵的特点值与特点向量sm=eigen(a)sme=diag(1:3)svde=svd(e)svdeattach(svde)u%*%diag(d)%*%t(v)#与矩阵运算有关的函数#取维数a=diag(1:4)nrow(a)ncol(a)#矩阵的兼并x1=rbind(c(1,2),c(3,4)x2=x1+10x3=cbind(x1,x2)x3x4=rbind(x1,x2)x4cbind(1,x1)#矩阵的拉直a=matrix(1:6,ncol=2,dimnames=list(c(one,two,three),c(first,second),byrow=T

6、)as.vector(a)#apply函数apply(a,1,mean)apply(a,2,sum)tapply(1:5,factor(c(f,f,m,m,m),mean)#第二题#发生随机数x=rnorm(100,0,1)x#画随机数的直方图hist(x,freq=F)#核密度曲线density(x)lines(density(x),col=blue)#增加正态散布散布函数y=seq(-4,3,0.2)lines(y,dnorm(y,mean(x),sd(x),col=red)#画随机数的经历散布函数z=rnorm(50,0,1)plot(ecdf(z),do.p=F,verticals=T

7、)d=seq(-3,2,0.2)lines(d,pnorm(d,mean(z),sd(z),col=red)y=rpois(100,2)plot(ecdf(y),col=red,verticals=T,do.p=F)x=0:8lines(x,ppois(x,mean(y),col=blue)w=c(75,64,47.4,66.9,62.2,62.2,58.7,63.5,66.6,64.0,57.0,69.0,56.9,50.0,72.0)hist(w,freq=F)lines(density(w),col=blue)x=44:76lines(x,dnorm(x,mean(w),sd(w),co

8、l=red)plot(ecdf(w),do.p=F,verticals=T)lines(x,pnorm(x,mean(w),sd(w),col=red)#编写函数求随机数的种种描绘统计量data_outline=function(x)n=length(x)m=mean(x)v=var(x)s=sd(x)me=median(x)cv=100*s/mcss=sum(x-m)2)uss=sum(x2)R=max(x)-min(x)#样本极差R1=quantile(x,3/4)-quantile(x,1/4)#四分位差sm=s/sqrt(n)#样本规范误g1=n/(n-1)/(n-2)*sum(x-m

9、)3)/s3g2=n*(n+1)/(n-1)/(n-2)/(n-3)*sum(x-m)4)/s4-3*(n-1)2/(n-2)/(n-3)data.frame(N=n,Mean=m,Var=v,std_dev=s,Median=me,std_mean=sm,CV=cv,CSS=css,USS=uss,R=R,R1=R1,Skewness=g1,Kurtosis=g2,row.names=1)x=rnorm(100)data_outline(x)#第三题#r,p,q,drnorm(100,0,1)pnorm(1:5,0,1)dnorm(-3:3,0,1)qnorm(seq(0,1,0.25),0

10、,1)rbeta(100,2,2)rbinom(100,100,0.5)pbinom(1:100,100,0.5)dbinom(1:5,100,0.5)qbinom(seq(0,1,0.1),100,0.5)rchisq(100,1qchisq(seq(0,1,0.2),10)pchisq(1:10,10)dchisq(1:10,10)rexp(100,0.5)rpois(100,2)ppois(1:1000,2)dpois(1:100,2)runif(100,0,1)qunif(c(0,0.2,0.8),0,1)punif(seq(0,1,0.2),0,1)dunif(seq(0,1,0.0

11、1),0,1)rt(100,2)qt(0.8,2)pt(-3:3,2)dt(-3:3,2)rf(100,1,2)qf(0.8,1,2)#四相信区间#1#1sigma曾经明白interval_estimate1=function(x,side=0,sigma=1,alpha=0.05)xb=mean(x);n=length(x)if(side0)tmp=sigma/sqrt(n)*qnorm(1-alpha)a=xb-tmp;b=Infelsetmp=sigma/sqrt(n)*qnorm(1-alpha/2)a=xb-tmp;b=xb+tmpdata.frame(mean=xb,a=a,b=b

12、)x=rnorm(100,0,4)interval_estimate1(x,sigma=4,side=0)interval_estimate1(x,sigma=4,side=-1)interval_estimate1(x,sigma=4,side=1)#2sigma未知interval_estimate2=function(x,side=0,alpha=0.05)xb=mean(x);n=length(x)if(side0)tmp=sd(x)/sqrt(n)*qt(1-alpha,n-1)a=xb-tmp;b=Infelsetmp=sd(x)/sqrt(n)*qt(1-alpha/2,n-1)

13、a=xb-tmp;b=xb+tmpdata.frame(mean=xb,a=a,b=b)x=rnorm(100,0,1)interval_estimate2(x,side=-1)interval_estimate2(x,side=0)interval_estimate2(x,side=1)t.test(x,side=-1)t.test(x,side=0)t.test(x,side=1)#两个总体sigma1=sigma2但未知interval_estimate3=function(x,y,alpha=0.05)xb=mean(x);yb=mean(y)n1=length(x);n2=lengt

14、h(y)sw=(n1-1)*var(x)+(n2-1)*var(y)/(n1+n1-2)tmp=sqrt(1/n1+1/n2)*sw)*qt(1-alpha/2,n1+n2-2)a=xb-yb-tmp;b=xb-yb+tmpdata.frame(mean=xb-yb,a=a,b=b)x=rnorm(100,0,1)y=rnorm(100,1,1)interval_estimate3(x,y)t.test(x,y)-0.03643479-0.98699097#第五题假定测验#1sigam曾经明白,双侧,测验mu=mu0mean.test1=function(x,mu=0,sigma=1)xb=m

15、ean(x);n=length(x)z=(xb-mu)/sigma*sqrt(n)p=pnorm(z)if(p=1/2)P=2*pelseP=2*(1-p)data.frame(mean=xb,Z=z,p_value=P)x=rnorm(100,0,2)mean.test1(x,mu=0,sigma=2)#(2)sigma未知,双侧,测验mu=mu0mean.test2=function(x,mu=0)xb=mean(x);n=length(x)z=(xb-mu)/sd(x)*sqrt(n)p=pt(z,n-1)if(p=1/2)P=2*pelseP=2*(1-p)data.frame(mea

16、n=xb,Z=z,p_value=P)x=rnorm(100)mean.test2(x,mu=0)t.test(x,mu=0,alt=two.side)#两个总体sigma1=sigma2但未知,测验mu1=mu2mean.test3=function(x,y,mu=0)xb=mean(x);yb=mean(y)n1=length(x);n2=length(y)sw=(n1-1)*var(x)+(n2-1)*var(y)/(n1+n2-2)t=(xb-yb-mu)/sqrt(sw*(1/n1+1/n2)p=pt(t,n1+n2-1)if(p=50),100)y=rbinom(100,100,0

17、.4)binom.test(sum(xy),length(x),alt=g)#第七题#相干性测验x=1:6y=6:1z=2:7cor.test(x,y,alt=g,method=spearman)cor.test(x,z,alt=g,method=spearman)#无节点x=c(2,3,1,4,5,8,6)y=1:7cor.test(x,y,alt=g,method=spearman,correct=T)n=length(x)r=rank(x)rR=rank(y)Rs=sum(r-R)2)rho=1-6*s/n/(n2-1)rho#有节点x=c(2,3,4,4,5,8,6)y=1:7cor.

18、test(x,y,alt=g,method=spearman,correct=T)exact=F,n=length(x)r=rank(x)rR=rank(y)Rsxy=sum(r*R)sx=sum(r2)sy=sum(y2)t=n*(n+1)/2)2rho=(sxy-t)/sqrt(sx-t)/sqrt(sy-t)rho#第八题回归x=c(seq(0.1,0.18,0.01),0.20,0.21,0.23)y=c(42,43.5,45,45.5,45,47.5,49,53,50,55,55,60)#散点图plot(x,y)#做回归lm.sol=lm(yx)lm.sol=lm(y1+x)#汇总统

19、计量summary(lm.sol)#画回归线abline(lm.sol)#求回归系数的区间估量beta.int=function(lm.sol,alpha=0.05)A=summary(lm.sol)$coefficientsdf=lm.sol$df.residualleft=A,1-A,2*qt(1-alpha/2,df)right=A,1+A,2*qt(1-alpha/2,df)rowname=dimnames(A)1#列表的第一个元素colname=c(estimate,left,right)matrix(c(A,1,left,right),ncol=3,dimnames=list(ro

20、wname,colname)beta.int(lm.sol)#对新的自变量求因变量的猜测值及猜测区间new=data.frame(x=c(0.16,0.19,1.20)lm.predict=predict(lm.sol,new)lm.predictlm.predict=predict(lm.sol,new,interval=confidence,level=0.95)lm.predict=predict(lm.sol,new,interval=prediction,level=0.95)#残差图resid=lm.sol$residualsplot(resid)y.res=resid(lm.sol)y.fit=predict(lm.sol)plot(y.resy.fit)plot(y.resx)plot(lm.sol,1)plot(lm.sol,2)plot(lm.sol,3)

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

当前位置:首页 > 考试专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服