版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、#期末考試專項復(fù)習(xí) #一、矩陣與數(shù)據(jù)框#1.生成特定的矩陣與數(shù)據(jù)框#矩陣#方法一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
2、=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,byrow=F,dimnames=list(1:2,c("one","two","three","four","five")#數(shù)據(jù)框的生成
3、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(
4、"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)LstLst"Name"Lst"Name"
5、;Lst1Lst1Lst$Namedf=as.data.frame(Lst)dfx=array(1:6,dim=c(2,3)as.data.frame(x)#數(shù)據(jù)框的引用df1:2,3:5df"Height"df$Weightnames(df)#此屬性一定非空rownames(df)=c("one","two","three","four","five")dfattach(df)r=Height/Weightrdf$r=rnames(df)detach()r=Height/W
6、eight#2.矩陣的運算a=diag(1:3)a21=1a#1轉(zhuǎn)置運算t(a)#2行列式det(a)#3向量內(nèi)積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:3solve(a,b)#ax=b的解#矩陣的特
7、征值與特征向量sm=eigen(a)sme=diag(1:3)svde=svd(e)svdeattach(svde)u%*%diag(d)%*%t(v)#與矩陣運算有關(guān)的函數(shù)#取維數(shù)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("fi
8、rst","second"),byrow=T)as.vector(a)#apply函數(shù)apply(a,1,mean)apply(a,2,sum)tapply(1:5,factor(c("f","f","m","m","m"),mean)#第二題#產(chǎn)生隨機數(shù)x=rnorm(100,0,1)x#畫隨機數(shù)的直方圖hist(x,freq=F)#核密度曲線density(x)lines(density(x),col="blue")#添加正態(tài)分布分布函數(shù)y=
9、seq(-4,3,0.2)lines(y,dnorm(y,mean(x),sd(x),col="red")#畫隨機數(shù)的經(jīng)驗分布函數(shù)z=rnorm(50,0,1)plot(ecdf(z),do.p=F,verticals=T)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&quo
10、t;)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),col="red")plot(ecdf(w),do.p=F,verticals=T)lines(x,pnorm(x,mean(w),sd(w),col="red")#編寫函數(shù)求隨機數(shù)的各種描述統(tǒng)計量data_outline=f
11、unction(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)#樣本標(biāo)準(zhǔn)誤g1=n/(n-1)/(n-2)*sum(x-m)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,
12、Median=me,std_mean=sm,CV=cv,CSS=css,USS=uss,R=R,R1=R1,Skewness=g1,Kurtosis=g2,s=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,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)rch
13、isq(100,1)qchisq(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.01),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)#四置信區(qū)間#1#(1)sigma已知interval_estimate1=f
14、unction(x,side=0,sigma=1,alpha=0.05)xb=mean(x);n=length(x)if(side<0)tmp=sigma/sqrt(n)*qnorm(1-alpha)a=-Inf;b=xb+tmpelse if(side>0)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)x=rnorm(100,0,4)interval_estimate1(x
15、,sigma=4,side=0)interval_estimate1(x,sigma=4,side=-1)interval_estimate1(x,sigma=4,side=1)#(2)sigma未知interval_estimate2=function(x,side=0,alpha=0.05)xb=mean(x);n=length(x)if(side<0)tmp=sd(x)/sqrt(n)*qt(1-alpha,n-1)a=-Inf;b=xb+tmpelse if(side>0)tmp=sd(x)/sqrt(n)*qt(1-alpha,n-1)a=xb-tmp;b=Infelse
16、tmp=sd(x)/sqrt(n)*qt(1-alpha/2,n-1)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
17、(x);yb=mean(y)n1=length(x);n2=length(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#第五題假設(shè)檢驗#(1)sigam已知,雙側(cè),檢驗mu=mu0me
18、an.test1=function(x,mu=0,sigma=1)xb=mean(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未知,雙側(cè),檢驗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)
19、if(p<=1/2)P=2*pelseP=2*(1-p)data.frame(mean=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
20、*(1/n1+1/n2)p=pt(t,n1+n2-1)if(p<=1/2)P=2*pelseP=2*(1-p)data.frame(mean=xb-yb,T=t,p_value=P)x=rnorm(100,0,1)y=rnorm(100,2,1)mean.test3(x,y,mu=-2)t.test(x,y,var.equal=T,mu=-2)x=rnorm(100,0,1)y=rnorm(100,0,2)mean.test3(x,y)t.test(x,y,var.equal=T)#第六題調(diào)用R函數(shù)#k-s檢驗兩組數(shù)是否同分布x=rnorm(100,0,1)y=rt(100,5)z=rn
21、orm(100,0,1)ks.test(x,y),alt="l"ks.test(x,z)#檢驗一組數(shù)是否服從已知分布ks.test(x,"pnorm",0,2)ks.test(x,"pt",1)#符號檢驗兩組數(shù)是否有差異x=rbinom(100,100,0.5)binom.test(sum(x>=50),100)y=rbinom(100,100,0.4)binom.test(sum(x<y),length(x),alt="g"#wilcoxon符號秩和檢驗(精確或大樣本近似)#wilcox.test(x
22、,y,alt,mu,paired=F,exact=NULL,correct=T,=F,conf.level=0.95)r=runif(100,136,145)wilcox.test(r,mu=140,alt="l",exact=F,=T,correct=F)x=rnorm(100)y=rnorm(100)wilcox.test(x,y,paired=T,alt="g")wilcox.test(x-y,alt="g")binom.test(sum(x>y),length(x),alt="g
23、")#第七題#相關(guān)性檢驗x=1:6y=6:1z=2:7cor.test(x,y,alt="g",method="spearman")cor.test(x,z,alt="g",method="spearman")#無節(jié)點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
24、/(n2-1)rho#有節(jié)點x=c(2,3,4,4,5,8,6)y=1:7cor.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)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030年中國高壓玻璃鋼管道市場發(fā)展現(xiàn)狀及前景規(guī)劃研究報告
- 2025-2030年中國鞋楦產(chǎn)業(yè)運行狀況及未來發(fā)展趨勢分析報告
- 2025-2030年中國陶瓷工藝品行業(yè)市場發(fā)展趨勢及前景調(diào)研分析報告
- 二零二五年度增強現(xiàn)實(AR)應(yīng)用軟件開發(fā)類框架合同3篇
- 2025-2030年中國軌道交通電源系統(tǒng)產(chǎn)業(yè)發(fā)展?fàn)顩r分析前景規(guī)劃研究報告
- 供應(yīng)鏈戰(zhàn)略合作伙伴關(guān)系建設(shè)考核試卷
- 園區(qū)智慧停車系統(tǒng)優(yōu)化與用戶體驗考核試卷
- 娃娃玩具創(chuàng)新設(shè)計與用戶體驗考核試卷
- 2025年度虛擬干股分紅及虛擬現(xiàn)實游戲引擎開發(fā)合同
- 2025年度泵車租賃與運輸一體化服務(wù)合同
- 道德經(jīng)中德文對照版
- 公路工程隨機抽樣一覽表(路基路面現(xiàn)場測試隨機選點方法自動計算)
- 2021版中醫(yī)癥候醫(yī)保對應(yīng)中醫(yī)癥候醫(yī)保2
- 2023年山東省青島市中考化學(xué)試題(含答案解析)
- 商業(yè)計劃書(BP)產(chǎn)品與服務(wù)的撰寫秘籍
- 安徽華塑股份有限公司年產(chǎn) 4萬噸氯化石蠟項目環(huán)境影響報告書
- 公司章程(二個股東模板)
- 世界奧林匹克數(shù)學(xué)競賽6年級試題
- 藥用植物學(xué)-課件
- 文化差異與跨文化交際課件(完整版)
- 國貨彩瞳美妝化消費趨勢洞察報告
評論
0/150
提交評論