R语言讲义(包括各种回归)_第1页
R语言讲义(包括各种回归)_第2页
R语言讲义(包括各种回归)_第3页
R语言讲义(包括各种回归)_第4页
R语言讲义(包括各种回归)_第5页
已阅读5页,还剩189页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

R语言讲义,免费(没有权力和铜臭)资源公开,可改变代码(不是黑盒子,也不是吝啬鬼,透明是防止“腐败”的最好方式)容易学习。可编程以实行复杂的课题可扩展:通过数千个网上提供的适用于不同领域、不同目的、不同方法的软件包来实现你的目标。也可以把你的方法贡献出来功能强大(绘图功能,优秀的内在帮助系统,R社区的支持,不断更新,不断修正)没有任何一个商业软件有如此多和如此新的算法,世界应用统计学家大都把自己的方法首先以R来实现,并尽量放到R网站上一年多,R网站的软件包数量增加了两倍,从近1000个到近3000多个。大都都有关于计算、演示和输入输出方法的函数和例子数据除非得到巨额资助(或者永远使用盗版软件),没有理由在公立学校教授商业软件绝大多数美国统计研究生都会的语言(Berkeley统计和应用数学本科都开设R语言课)我的很大一部分数据分析知识的来源就是R.我都能学会,并且到处宣传和普及,相信你们会做得更好!,点击CRAN得到一批镜像网站,下载R(/),点击镜像网站比如Berkeley,Packages(每个都有大量数据和可以读写修改的函数/程序),baseTheRBasePackagebootBootstrapR(S-Plus)Functions(Canty)classFunctionsforClassificationclusterClusterAnalysisExtendedRousseeuwetal.concordConcordanceandreliabilitydatasetsTheRDatasetsPackageexactRankTestsExactDistributionsforRankandPermutationTestsforeignReadDataStoredbyMinitab,S,SAS,SPSS,Stata,Systat,dBase,.graphicsTheRGraphicsPackagegrDevicesTheRGraphicsDevicesandSupportforColoursandFontsgridTheGridGraphicsPackageKernSmoothFunctionsforkernelsmoothingforWandsample(1:10,3)#随机种子!z=sample(1:200000,10000)z1:10#向量下标y=c(1,3,7,3,4,2)zy,一些基本运算例子,z=sample(x,20,rep=T)z(z1=unique(z);length(z1)z=sample(x,100,rep=T)xz=setdiff(x,z)sort(union(xz,z)sort(union(xz,z)=xsetequal(union(xz,z),x)intersect(1:10,7:50)sample(1:100,20,prob=1:100),一些基本运算例子,pi*102#能够用?”*”来看基本算术运算方法*(pi,(10,2)pi*(1:10)2x-pi*102xprint(x)(x=pi*102)pi(1:5)print(x,digits=12)class(x)typeof(x),一些基本运算例子,class(cars)typeof(cars)names(cars)summary(cars)str(cars)s(cars)class(distspeed)plot(distspeed,cars),一些基本运算例子,head(cars)#cars1:6,tail(cars)ncol(cars);nrow(cars)dim(cars)lm(distspeed,data=cars)cars$qspeed=cut(cars$speed,breaks=quantile(cars$speed),include.lowest=TRUE)names(cars)cars3table(cars3)is.factor(cars$qspeed)plot(distqspeed,data=cars)(a=lm(distqspeed,data=cars)summary(a),一些基本运算例子,x-round(runif(20,0,20),digits=2)summary(x)min(x);max(x)median(x)#medianmean(x)#meanvar(x)#variancesd(x)#standarddeviationsqrt(var(x)rank(x)#rankorder(x)xorder(x)sort(x)sort(x,decreasing=T)#sort(x,dec=T)sum(x);length(x)round(x),一些基本运算例子,fivenum(x)#quantilesquantile(x)#quantiles(differentconvention)有多种定义quantile(x,c(0,.33,.66,1)mad(x)#normalizedmeandeviationtothemedian(“medianaveragedistance“)可用?mad查看cummax(x)cummin(x)cumprod(x)cor(x,sin(x/20)#correlation,一些基本运算例子,#直方图x-rnorm(200)hist(x,col=lightblue)rug(x)#茎叶图stem(x)#散点图N-500 x-rnorm(N)y0);(1:10)x0diff(x)diff(x,lag=2)x=matrix(1:20,4,5);xx=matrix(1:20,4,5,byrow=T);xt(x)x=matrix(sample(1:100,20),4,5)2*xx+5y=matrix(sample(1:100,20),5,4)x+t(y)(z=x%*%y)z1=solve(z)#solve(a,b)可以解ax=b方程z1%*%zround(z1%*%z,14),矩阵,nrow(x);ncol(x);dim(x)#行列数目x=matrix(rnorm(24),4,6)xc(2,1),#第2和第1行x,c(1,3)#第1和第3列x2,1#第2,1元素xx,10,1#第1列大于0的元素sum(x,10)#第1列大于0的元素的个数sum(x,10apply(x,2,sum),矩阵/高维数组,#上下三角阵x=matrix(rnorm(24),4,6)diag(x)diag(1:5)diag(5)xlower.tri(x)=0#xupper.tri(x)=0;diag(x)=0 x=array(runif(24),c(4,3,2);xis.matrix(x)#可由dim(x)得到维数(4,3,2)is.matrix(x1,)x=array(1:24,c(4,3,2)xc(1,3),x=array(1:24,c(4,3,2)apply(x,1,mean)apply(x,1:2,sum)apply(x,c(1,3),prod),矩阵/高维数组/scale,#矩阵与向量之间的运算x=matrix(1:20,5,4)sweep(x,1,1:5,*)x*1:5sweep(x,2,1:4,+)(x=matrix(sample(1:100,24),6,4);(x1=scale(x)(x2=scale(x,scale=F);(x3=scale(x,center=F)round(apply(x1,2,mean),14)apply(x1,2,sd)round(apply(x2,2,mean),14);apply(x2,2,sd)round(apply(x3,2,mean),14);apply(x3,2,sd),Data.frame,x=matrix(1:6,2,3)z=data.frame(x);zz$X2attributes(z)names(z)=c(TOYOTA,GM,HUNDA)s(z)=c(2001,2002)Zattach(x)GMdetach(x)GMsapply(z,is.numeric)#apply(z,2,is.numeric),缺失值问题等,airqualitycomplete.cases(airquality)#哪一行没有缺失值which(complete.cases(airquality)=F)sum(complete.cases(airquality)na.omit(airquality)#append,cbind,vbindx=1:10;x12=3(x1=append(x,77,after=5)cbind(1:3,4:6);rbind(1:3,4:6)#去掉矩阵重复的行(x=rbind(1:5,runif(5),runif(5),1:5,7:11)x!duplicated(x),unique(x),List,#list可以是任何对象的集合(包括lists)z=list(1:3,Tom=c(1:2,a=list(R,letters1:5),w=hi!)z1;z2z$Tz$T$a2z$T3z$T$wattributes(airquality)#属性!airquality$Ozoneattributes(matrix(1:6,2,3),CategoricaldataAsurveyaskspeopleiftheysmokeornot.ThedataisYes,No,No,Yes,Yesx=c(Yes,No,No,Yes,Yes)table(x);xfactor(x),Barplot:Suppose,agroupof25peoplearesurveyedastotheirbeer-drinkingpreference.Thecategorieswere(1)Domesticcan,(2)Domesticbottle,(3)Microbrewand(4)import.Therawdatais3411343313212123231111431beer=scan()3411343313212123231111431barplot(beer)#thisisntcorrectbarplot(table(beer)#Yes,callwithsummarizeddatabarplot(table(beer)/length(beer)#dividebynforproportiontable(beer)/length(beer),Table/categoricaldata,library(MASS)quineattach(quine)table(Age)table(Sex,Age);tab=xtabs(Sex+Age,quine);unclass(tab)tapply(Days,Age,mean)tapply(Days,list(Sex,Age),mean)#apply,sapply,tapply,lapply,smokes=c(Y,N,N,Y,N,Y,Y,Y,N,Y)amount=c(1,2,2,3,3,1,2,1,3,2)(tmp=table(smokes,amount)#storethetableoptions(digits=3)#onlyprint3decimalplacesprop.table(tmp,1)#therowssumto1nowprop.table(tmp,2)#thecolumnssumto1now#上两行等价于下面两行sweep(tmp,1,margin.table(tmp,1),/)sweep(tmp,2,margin.table(tmp,2),/)prop.table(tmp)#amount#allthenumberssumto1options(digits=7)#restorethenumberofdigits,array/matrixtabledata.frame,#Startwithacontingencytable.ftable(Titanic,row.vars=1:3)ftable(Titanic,row.vars=1:2)data.frame(Titanic)#把array变成data.framea=xtabs(FreqSurvived+Sex,w)biplot(corresp(a,nf=2)#应用之一#Startwithadataframe.str(mtcars)x-ftable(mtcarsc(cyl,vs,am,gear)x#为array,其维的次序为(cyl,vs,am,gear)ftable(x,row.vars=c(2,4)#从x(array)确定表的行变量#Startwithexpressions,usetable()sdnntochangelabelsftable(mtcars$cyl,mtcars$vs,mtcars$am,mtcars$gear,row.vars=c(2,4),dnn=c(Cylinders,V/S,Transmission,Gears)ftable(vscarb,mtcars)#vs是列,carb是行#或ftable(mtcars$vsmtcars$carb)ftable(carbvs,mtcars)#vs是行,carb是列ftable(mtcars,c(8,11)#和上面ftable(carbvs,mtcars)等价ftable(breakswool+tension,warpbreaks)#as.data.frame(DF-as.data.frame(UCBAdmissions)#等价于data.frame(UCBAdmissions)xtabs(FreqAdmit+Gender+Dept,DF)#:把方阵变成原来的列联表(a=xtabs(FreqAdmit+Gender,data=DF)#如无频数(权),左边为空,写函数,ss=function(n=100)z=2;for(iin2:n)if(any(i%2:(i-1)=0)=F)z=c(z,i);return(z)fix(ss)ss()t1=Sys.time()ss(10000)Sys.time()-t1system.time(ss(10000)#函数可以不写return,这时最后一个值为return的值.为了输出多个值最好使用list,#几个图一起:par(mfrow=c(2,4)#par(mfcol=c(2,4)layout(matrix(c(1,1,1,2,3,4,2,3,4),nr=3,byrow=T)hist(rnorm(100),col=Red,10)hist(rnorm(100),col=Blue,8)hist(rnorm(100),col=Green)hist(rnorm(100),col=Brown)#par(mar=c(bottom,left,top,right)设置边缘#缺省值c(5,4,4,2)+0.1(英寸)spring=data.frame(compression=c(41,39,43,53,42,48,47,46),distance=c(120,114,132,157,122,144,137,141)attach(spring)#(Hookeslaw:f=.5ks)par(mfcol=c(2,2)plot(distancecompression)plot(distancecompression,type=l)plot(compression,distance,type=o)plot(compression,distance,type=b),关于画图,关于画图,par(mfrow=c(2,2)#准备画2x2的4个图plot(compression,distance,main=HookesLaw)#只有标题plot(compression,distance,main=HookesLaw,xlab=x,ylab=y)#标题+x,y标记identify(compression,distance)#标出点号码plot(compression,distance,main=HookesLaw)#只有标题text(46,120,expression(f=frac(1,2)*k*s)#在指定位写入文字plot(compression,distance,main=HookesLaw)#只有标题的图text(locator(2),c(Iamhere!,youarethere!)#在点击的两个位置写入文字par(mfrow=c(1,1)plot(1:10,sin(1:10),type=l,lty=2,col=4,main=paste(strwrap(Thetitleistoolong,andIhatetomakeitshorter,!#$%data(Animals);attach(Animals)par(mfrow=c(2,2)plot(body,brain)plot(sqrt(body),sqrt(brain)plot(body)0.1,(brain)0.1)plot(log(body),log(brain)#或者plot(brainbody,log=xy)par(mfrow=c(1,1)par(cex=0.7,mex=0.7)#character(cex)barplot(rnorm(15,10,3),col=1:15)palette(rainbow(15);barplot(rnorm(15,10,3),col=1:15)palette(heat.colors(15);barplot(rnorm(15,10,3),col=1:15)palette(terrain.colors(15);barplot(rnorm(15,10,3),col=1:15)palette(topo.colors(15);barplot(rnorm(15,10,3),col=1:15)palette(cm.colors(15);barplot(rnorm(15,10,3),col=1:15)palette(gray(seq(0,0.9,l=15);barplot(rnorm(15,10,3),col=1:15)palette(grey(seq(0,0.5,l=15);barplot(rnorm(15,10,3),col=1:15)palette(default)par(mfrow=c(1,1),关于画图,#matplotsines=outer(1:20,1:4,function(x,y)sin(x/20*pi*y)matplot(sines,pch=1:4,type=o,col=rainbow(ncol(sines)#legendx-seq(-pi,pi,len=65)plot(x,sin(x),type=l,ylim=c(-1.2,1.8),col=3,lty=2)points(x,cos(x),pch=3,col=4)lines(x,tan(x),type=b,lty=1,pch=4,col=6)title(legend(.,lty=c(2,-1,1),pch=c(-1,3,4),merge=TRUE),cex.main=1.1)legend(-1,1.9,c(sin,cos,tan),col=c(3,4,6),lty=c(2,-1,1),pch=c(-1,3,4),merge=TRUE,bg=gray90),关于画图,#barplotandtablepar(mfrow=c(2,2)tN=table(Ni=rpois(100,lambda=5);tNr=barplot(tN,col=gray)lines(r,tN,type=h,col=red,lwd=2)#-type=hplotting*is*barplotbarplot(tN,space=1.5,axisnames=FALSE,sub=barplot(.,space=0,axisnames=FALSE)#如space=1.5则有稀牙缝barplot(tN,space=0,axisnames=FALSE,sub=barplot(.,space=0,axisnames=FALSE)pie(tN)#pieplotpar(mfrow=c(1,1)#加gridplot(1:3)grid(10,5,lwd=2)dev.set;dev.off;dev.list,关于画图(pairs/三维),#pairs#data(iris)pairs(iris1:4,main=AndersonsIrisData-3species,pch=21,bg=c(red,green3,blue)unclass(iris$Species)#iris为150 x5数据,这里是4个数量变量的点图(最后一个是分类变量(iris$Species)#stars#data(mtcars)stars(mtcars,1:7,key.loc=c(14,1.5),main=MotorTrendCars:fullstars(),flip.labels=FALSE)#mtcars为32x11数据,这里只选前7个数量变量的点图#perspx-seq(-10,10,length=30)y-xf-function(x,y)r-sqrt(x2+y2);10*sin(r)/rz-outer(x,y,f)zis.na(z)-1persp(x,y,z,theta=30,phi=30,expand=0.5,col=lightblue),data(volcano)par(mfrow=c(2,2)z-2*volcano#Exaggeratethereliefx-10*(1:nrow(z)#10meterspacing(StoN)y-10*(1:ncol(z)#10meterspacing(EtoW)#Dontdrawthegridlines:border=NA#par(bg=slategray)persp(x,y,z,theta=135,phi=30,col=green3,scale=FALSE,ltheta=-120,shade=0.75,border=NA,box=FALSE)par(bg=white)#contourrx-range(x-10*1:nrow(volcano)ry-range(y-10*1:ncol(volcano)ry-ry+c(-1,1)*(diff(rx)-diff(ry)/2tcol-terrain.colors(12)opar-par(pty=s,bg=lightcyan);par(opar)plot(x=0,y=0,type=n,xlim=rx,ylim=ry,xlab=,ylab=)u-par(usr)rect(u1,u3,u2,u4,col=tcol8,border=“red”)#rect画矩形contour(x,y,volcano,col=tcol2,lty=solid,add=TRUE,vfont=c(sansserif,plain)title(ATopographicMapofMaungaWhau,font=4)abline(h=200*0:4,v=200*0:4,col=lightgray,lty=2,lwd=0.1);par(opar)#imagex-10*(1:nrow(volcano)y-10*(1:ncol(volcano)image(x,y,volcano,col=terrain.colors(100),axes=FALSE)contour(x,y,volcano,levels=seq(90,200,by=5),add=TRUE,col=peru)axis(1,at=seq(100,800,by=100)axis(2,at=seq(100,600,by=100)box()title(main=MaungaWhauVolcano,font.main=4)par(mfrow=c(1,1),关于画图(三维),多窗口操作,x11()plot(1:10)x11()plot(rnorm(10)dev.set(dev.prev()abline(0,1)#throughthe1:10pointsdev.set(dev.next()abline(h=0,col=gray)#fortheresidualplotdev.set(dev.prev()dev.off();dev.off()#-closethetwoXdevices#dev.list(),画图杂项,#模拟布朗运动n=100;x=cumsum(rnorm(100);y=cumsum(rnorm(100);plot(x,y,type=l)x=0;y=0;plot(100,ylim=c(-15,15),xlim=c(-15,15)#慢动作for(iin1:200)x1=x+rnorm(1);y1=y+rnorm(1);segments(x,y,x1,y1);x=x1;y=y1Sys.sleep(.05)#散点大小同因变量值成比例x=1:10;y=runif(10)symbols(x,y,circle=y/2,inches=F,bg=x)#数据框的每一列都做Q-Q图table=data.frame(x1=rnorm(100),x2=rnorm(100,1,1)par(ask=TRUE)#waitforchanging等待页面改变的确认results=apply(table,2,qqnorm)par(ask=FALSE)#在一个图上添加一个小图x=rnorm(100)hist(x)op=par(fig=c(.02,.5,.5,.98),new=TRUE)boxplot(x)#数学符号x=1:10;plot(x,type=n)text(3,2,expression(paste(Temperature(,degree,C)in2003)text(4,4,expression(bar(x)=sum(frac(xi,n),i=1,n)text(6,6,expression(hat(beta)=(Xt*X).1*Xt*y)text(8,8,expression(zi=sqrt(xi2+yi2),改变大小写字母,x=c(I,am,A,BIG,Cat)tolower(x)toupper(x),R统计模型讲义,#基础x=rnorm(20,10)t.test(x,m=9,alt=greater)t.test(x1:10,m=9,alt=greater)$p.valuet.test(x,con=.90)$confx=rnorm(30,10);y=rnorm(30,10.1)t.test(x,y,alt=less)library(TeachingDemos)ci.examp()run.ci.examp()vis.boxcox()vis.boxcoxu(),回归,#相关x=rnorm(20);y=rnorm(20);cor(x,y)cor(x,y,method=kendall);cor(x,y,method=spearman)cor.test(x,y);cor.test(x,y,method=kendall);cor.test(x,y,method=spearman)cor.test(x,y,method=kendall)$p.value#相关吗?x=rnorm(3);y=rnorm(3);cor(x,y);cor.test(x,y)$p.valuelibrary(TeachingDemos)put.points.demo(),相关,基本原理,#基本原理set.seed(100)x1=rnorm(100);x2=rnorm(100);eps=rnorm(100)y=5+2*x1-3*x2+epsa=lm(yx1+x2)(lm(y0+x1+x2)#不要截距:等价于(lm(y-1+x1+x2)summary(a);anova(a)names(a)shapiro.test(a$res)qqnorm(a$res);qqline(a$res)#数学原理x=cbind(1,x1,x2)dim(x)b=solve(t(x)%*%x)%*%t(x)%*%yba$coe,63,例1:cross.txt,例1:cross.txt,w=read.table(cross.txt,header=T)head(w)plot(yx,w);summary(w)a=lm(yx+z,w)summary(a)anova(a)qqnorm(a$res);qqline(a$res)shapiro.test(a$res)a1=lm(yx*z,w)summary(a1);anova(a1)qqnorm(a1$res);qqline(a1$res)shapiro.test(a1$res)anova(a,a1)library(party)#更简单的方法wt=mob(yx|z,data=w)coef(wt);plot(wt)plot(yx,w);abline(coef(wt)1,col=2);abline(coef(wt)2,col=4),65,回归方程,66,PoisonExperimentThedatagivethesurvivaltimes(in10hourunits)ina3x4factorialexperiment,thefactorsbeing(a)threepoisonsand(b)fourtreatments.Eachcombinationofthetwofactorsisusedforfouranimals,theallocationtoanimalsbeingcompletelyrandomized.Box,G.E.P.,andCox,D.R.(1964).Ananalysisoftransformations(withDiscussion).J.R.Statist.Soc.B,26,211-252./data/general/poison.html,67,例2:poison.txt:3种毒药,4种处理,用于动物实验,48个观测值,回归,setwd(f:/2010stat)w=read.table(poison.txt,head=T)head(w);tail(w)str(w);summary(w)dim(w)w$Poison=factor(w$Poison)w$Treatment=factor(w$Treatment)pairs(w)#直接回归a=lm(TimePoison*Treatment,w)anova(a)a=lm(Time.,w)anova(a)qqnorm(a$res);qqline(a$res)shapiro.test(a$res)#变换a=lm(1/TimePoison+Treatment,w)anova(a)qqnorm(a$res);qqline(a$res)shapiro.test(a$res)summary(a),逐步回归(step或stepAICMASS),library(MASS)quine.hi-aov(log(Days+2.5).4,quine)quine.nxt-update(quine.hi,.-Eth:Sex:Age:Lrn)quine.stp-stepAIC(quine.nxt,scope=list(upper=Eth*Sex*Age*Lrn,lower=1),trace=FALSE)quine.stp$anovacpus1-cpusattach(cpus)for(vinnames(cpus)2:7)cpus1v-cut(cpusv,unique(quantile(cpusv),include.lowest=TRUE)detach()cpus0-cpus1,2:8#excludesnames,authorspredictionscpus.samp-sample(1:209,100)cpus.lm-lm(log10(perf).,data=cpus1cpus.samp,2:8)cpus.lm2-stepAIC(cpus.lm,trace=FALSE)cpus.lm2$anova,example(birthwt)birthwt.glm-glm(low.,family=binomial,data=bwt)birthwt.step-stepAIC(birthwt.glm,trace=FALSE)birthwt.step$anovabirthwt.step2-stepAIC(birthwt.glm,.2+I(scale(age)2)+I(scale(lwt)2),trace=FALSE)birthwt.step2$anovaquine.nb-glm.nb(Days.4,data=quine)quine.nb2-stepAIC(quine.nb)quine.nb2$anova,71,变换并拟合主效应,72,结果解释,异方差时的考虑,Box-Cox变换加权最小二乘(如:lm中weights=1/x2)异常值处理不一定是一个模型等等(要做探索分析),多项式回归,#多项式回归y-cars$dist;x-cars$speedo=order(x)plot(yx)do.it-function(model,col)r-lm(model);yp-predict(r)lines(ypoxo,col=col,lwd=3)do.it(yx,col=red)do.it(yx+I(x2),col=blue)do.it(y-1+I(x2),col=green)legend(par(usr)1,par(usr)4,c(affinefunction,degree-2polynomial,degree2monomial),lwd=3,col=c(red,blue,green),)n-100 x-runif(n,min=-4,max=4)+sign(x)*.2y-1/x+rnorm(n)#双曲线plot(yx)lm(1/yx)n-100 x-rlnorm(n)3.14#alog-normaldistributionisaprobabilitydistributionofarandomvariablewhoselogarithmisnormallydistributed.y-x-.1*rlnorm(n)plot(yx)lm(log(y)log(x),多项式p,q正交,如,#关于正交多项式y-cars$dist;x-cars$speed#非正交:一项加一项(互相影响,显著的系数变成不显著)summary(lm(yx)summary(lm(yx+I(x2)summary(lm(yx+I(x2)+I(x3)summary(lm(yx+I(x2)+I(x3)+I(x4)summary(lm(yx+I(x2)+I(x3)+I(x4)+I(x5)#正交:不会改变开始显著的系数poly:ComputeOrthogonalPolynomialssummary(lm(ypoly(x,1)summary(lm(ypoly(x,2)summary(lm(ypoly(x,3)summary(lm(ypoly(x,4)summary(lm(ypoly(x,5),#对正交多项式点出系数的p-valuesn-5p-matrix(nrow=n,ncol=n+1)for(iin1:n)pi,1:(i+1)-summary(lm(ypoly(x,i)$coefficients,4matplot(p,type=l,lty=1,lwd=3)legend(par(usr)1,par(usr)4,as.character(1:n),lwd=3,lty=1,col=1:n)title(main=Evolutionofthep-values(orthonormalpolynomials)#对非正交多项式,点出系数的p-valuesp-matrix(nrow=n,ncol=n+1)p1,1:2-summary(lm(yx)$coefficients,4p2,1:3-summary(lm(yx+I(x2)$coefficients,4p3,1:4-summary(lm(yx+I(x2)+I(x3)$coefficients,4p4,1:5-summary(lm(yx+I(x2)+I(x3)+I(x4)$coefficients,4p5,1:6-summary(lm(yx+I(x2)+I(x3)+I(x4)+I(x5)$coefficients,4matplot(p,type=l,lty=1,lwd=3)legend(par(usr)1,par(usr)4,as.character(1:n),lwd=3,lty=1,col=1:n)title(main=Evolutionofthep-values(nonorthonormalpolynomials)“),#例子data(beavers)y-beaver1$tempx-1:length(y)plot(yx)for(iin1:10)r-lm(ypoly(x,i)lines(predict(r),type=l,col=i)summary(r),非参数回归,#非参数:样条plot(quakes$long,quakes$lat)lines(smooth.spline(quakes$long,quakes$lat),col=red,lwd=3)library(Design)#rcs:DesignSpecialTransformationFunctions#4-nodespliner3-lm(quakes$latrcs(quakes$long)plot(quakes$latquakes$long)o-order(quakes$long)lines(quakes$longo,predict(r)o,col=red,lwd=3)r-lm(quakes$latrcs(quakes$long,10)lines(quakes$longo,predict(r)o,col=blue,lwd=6,lty=3)title(main=Regressionwithrcs)legend(par(usr)1,par(usr)3,yjust=0,c(4knots,10knots),lwd=c(3,3),lty=c(1,3),col=c(red,blue)#更多的样条library(splines)data(quakes)x-quakes,2y-quakes,1o-order(x)x-xoy-yor1-lm(ybs(x,df=10)r2-lm(yns(x,df=6)plot(yx)lines(predict(r1)x,col=red,lwd=3)lines(predict(r2)x,col=green,lwd=3),#核光滑plot(cars$speed,cars$dist)lines(ksmooth(cars$speed,cars$dist,normal,bandwidth=2),col=red)lines(ksmooth(cars$speed,cars$dist,normal,bandwidth=5),col=green)lines(ksmooth(cars$speed,cars$dist,normal,bandwidth=10),col=blue)#加权局部最小二乘.WeightedLocalLeastSquares:loess#各种核函数curve(dnorm(x),xlim=c(-3,3),ylim=c(0,1.1)x-seq(-3,3,length=200)D.Epanechikov-function(t)ifelse(abs(t)1,3/4*(1-t2),0)lines(D.Epanechikov(x)x,col=red)D.tricube-function(t)#akatriweightkernelifelse(abs(t)1,(1-abs(t)3)3,0)lines(D.tricube(x)x,col=blue)legend(par(usr)1,par(usr)4,yjust=1,c(noyaugaussien,noyaudEpanechikov,noyautricub

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论