R语言(介绍)

上传人:猪** 文档编号:242969042 上传时间:2024-09-13 格式:PPT 页数:74 大小:1.15MB
返回 下载 相关 举报
R语言(介绍)_第1页
第1页 / 共74页
R语言(介绍)_第2页
第2页 / 共74页
R语言(介绍)_第3页
第3页 / 共74页
点击查看更多>>
资源描述
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,R-,语言,/,软件,R,免费,R,资源公开,(,不是黑盒子,也不是吝啬鬼,),R,可以在,UNIX,Linux,Windows,和,OS X,运行,.,R,有优秀的,内在帮助系统,.,R,有优秀的,画图功能,学生能够轻松地,转,到商业支持的,S-Plus,程序,(,如果需要使用商业软件,),R,语言有一个,强大的,容易学习,的语法,有许多内在的统计函数,.,R,是计算机编程语言,.,类似于,UNIX,语言,C,语言,Pascal,Gauss,语言等,.,对于熟练的编程者,它将觉得该语言,比其他语言更熟悉,.,而对计算机初学者,学习,R,语言,使得学习下一步的其他编程不那么困难,.,那些傻瓜软件,(,SAS,SPSS,等,),语言的语法则完全不同,.,通过用户,自编程序, R,语言很容易,延伸和扩大,.,它就是这样成长的,.,R,的缺点,不如,S-Plus,在编辑输出的画图那样好,没有商业支持,(,但有网上支持,),需要编程,不傻瓜,.,使用编程软件容易吗?,以,R,软件为例,.,非常好学,极易入门,基本功能简单,和写公式一样,能在一个小时学会基本运算语句,在一天内学会编写函数,(,子程序,),和利用软件包,.,往往困难的反而是统计内容本身,R,的历史,S,语言在,1980,年代后期在,AT&T,实验室开发,.,R,项目由,Auckland,大学统计系的,Robert Gentleman,和,Ross Ihaka,于,1995,年开始的,.,它很快得到广泛用户的欢迎,.,目前它是由,R,核心发展团队维持,;,它是一个由志愿者组成的工作努力的国际团队,.,点击,CRAN,得到一批镜像网站,下载,R(http:/www.r-project.org/),点击镜像网站比如,ctex,选择这个,下载安装文件,选择,base,R,里面有什么,?,Packages,(每个都有大量数据和可以读写修改的函数,/,程序),base,The R Base Package,boot,Bootstrap R (S-Plus) Functions (Canty),class,Functions for Classification,cluster,Cluster Analysis Extended Rousseeuw et al.,concord,Concordance and reliability,datasets,The R Datasets Package,exactRankTests,Exact Distributions for Rank and Permutation Tests,foreign,Read Data Stored by Minitab, S, SAS, SPSS, Stata, Systat, dBase, .,graphics,The R Graphics Package,grDevices,The R Graphics Devices and Support for Colours and Fonts,grid,The Grid Graphics Package,KernSmooth,Functions for kernel smoothing for Wand & Jones (1995),lattice,Lattice Graphics Interface,tools,Tools for Package Development,utils,The R Utils Package,Packages,(继续),MASS,Main Package of Venables and Ripleys MASS,methods,Formal Methods and Classes,mgcv,GAMs with GCV smoothness estimation and GAMMs by REML/PQL,multtest,Resampling-based multiple hypothesis testing,nlme,Linear and nonlinear mixed effects models,nnet,Feed-forward Neural Networks and Multinomial Log-Linear Models,nortest,Tests for Normality,outliers,Tests for outliers,pls,Partial Least Squares Regression (PLSR) and Principal Component Regression (PCR),pls.pcr,PLS and PCR functions,rpart,Recursive Partitioning,SAGx,Statistical Analysis of the GeneChip,sma,Statistical Microarray Analysis,spatial,Functions for Kriging and Point Pattern Analysis,splines,Regression Spline Functions and Classes,stats,The R Stats Package,stats4,Statistical Functions using S4 Classes,survival,Survival analysis, including penalised likelihood.,tcltk,Tcl/Tk Interface,tools,Tools for Package Development,utils,The R Utils Package,Packages,(网上),网上还有,许多,选择这个,下载软件包,所有这些,Packages,都是在,base,和,stats package,上添加的,Base, stats,包含所有固有的应用和数据,而其他的,packages,包含各统计学家自己发展的方法和数据。,希望你是下一个加盟这些,packages,的作者之一。,R,的默认界面,第一章,R,入门,1,输入指令, sqrt(9),1 3, abs(-5),1 5,行前标号, z=seq(-1,10,length=100), z,1 -1.0000000 -0.8888889 -0.7777778 -0.6666667 -0.5555556 -0.4444444,7 -0.3333333 -0.2222222 -0.1111111 0.0000000 0.1111111 0.2222222,13 0.3333333 0.4444444 0.5555556 0.6666667 0.7777778 0.8888889,19 1.0000000 1.1111111 1.2222222 1.3333333 1.4444444 1.5555556,25 1.6666667 1.7777778 1.8888889 2.0000000 2.1111111 2.2222222,31 2.3333333 2.4444444 2.5555556 2.6666667 2.7777778 2.8888889,37 3.0000000 3.1111111 3.2222222 3.3333333 3.4444444 3.5555556,43 3.6666667 3.7777778 3.8888889 4.0000000 4.1111111 4.2222222,49 4.3333333 4.4444444 4.5555556 4.6666667 4.7777778 4.8888889,55 5.0000000 5.1111111 5.2222222 5.3333333 5.4444444 5.5555556,61 5.6666667 5.7777778 5.8888889 6.0000000 6.1111111 6.2222222,67 6.3333333 6.4444444 6.5555556 6.6666667 6.7777778 6.8888889,73 7.0000000 7.1111111 7.2222222 7.3333333 7.4444444 7.5555556,79 7.6666667 7.7777778 7.8888889 8.0000000 8.1111111 8.2222222,85 8.3333333 8.4444444 8.5555556 8.6666667 8.7777778 8.8888889,91 9.0000000 9.1111111 9.2222222 9.3333333 9.4444444 9.5555556,97 9.6666667 9.7777778 9.8888889 10.0000000,补充指令提醒, sqrt(16,+ ),1 4,2,变量与赋值,赋值, x,=,9, x,1 9, x, x,1 8, y=hello!, y,1 hello!,注意:,使用过的变量都会储存在,R,的工作空间,(workspace),中,使用者可以输入,objects(),来检查对象清单。要删除不需要的对象,如,x,,可用,remove(“x”),或,rm(x),。,3,向量,向量赋值,observations=c(1,2,3,4,5,6,7,8,9),数乘,2.54*observations,(,乘以每个元素,),均值和方差,mean(observations) var(observations),注意:,mean(),和,var(),自变量是向量。,mean(1,3,5),(,错误,),mean(c(1,3,5),(,正确,),4,从向量中选取子集,observations3,取第,3,个元素,observations5:7,取第,5,6,7,个元素,observationsc(1,2,7,1),取第,1,2,7,1,个元素,observationsobservations4,取大于,4,的元素,(, =, data(trees), names(trees),1 Girth Height Volume, dim(trees),1 31 3, trees $ Girth,1 8.3 8.6 8.8 10.5 10.7 10.8 11.0 11.0 11.1 11.2 11.3 11.4 11.4 11.7 12.0 12.9 12.9 13.3 13.7 13.8 14.0 14.2 14.5 16.0 16.3 17.3 17.5 17.9 18.0 18.0,31 20.6, trees $Height,1 70 65 63 72 81 83 66 75 80 75 79 76 76 69 75 74 85 86 71 64 78 80 74 72 77 81 82 80 80 80 87, trees $ Volume,1 10.3 10.3 10.2 16.4 18.8 19.7 15.6 18.2 22.6 19.9 24.2 21.0 21.4 21.3 19.1 22.2 33.8 27.4 25.7 24.9 34.5 31.7 36.3 38.3 42.6 55.4 55.7 58.3 51.5 51.0,31 77.0, vol=trees $ Girth2*trees $ Height/(4*pi), summary(vol),Min. 1st Qu. Median Mean 3rd Qu. Max.,382.6 750.2 969.9 1141.0 1375.0 2938.0, trees $ Volumevol2000,1 58.3 51.5 51.0 77.0,6 R,图像解说,茎叶图, stem(vol),The decimal point is 3 digit(s) to the right of the |,0 | 444,0 | 667778888889,1 | 00112223,1 | 569,2 | 0011,2 | 9,直方图,hist(vol, col=2),散点图,plot(trees $ Volum, vol),第二章 对象与数据类型,1,标量与向量:向量中的元素必须是同一类型,(,数值或字符,),。若一向量同时包含了数值和字符,,R,会把该向量视为字符处理。, blash=c(1,2,3,d), blash,1 1 2 3 d,向量的属性为,长度,。, length(blash),1 4, length(blash)=5, blash,1 1 2 3 d NA,2,因子,(Factor),男,女,小学生,初中生,高中生,因子的属性为水平,(levels(),。,例:,10,个病人分别用三种药,placebo, A, B,治疗。, medicine=c(rep(placebo,3),rep(A,4),rep(B,4), medicine,1 placebo placebo placebo A A A A,8 B B B B, medicine=as.factor(medicine), medicine,1 placebo placebo placebo A A A A B B B B,Levels: A B placebo, levels(medicine),1 A B placebo, medicine=ordered(medicine,levels=c(B,placebo,A), medicine,1 placebo placebo placebo A A A A B B B B,Levels: B placebo matrix(c(1:9),nrow=3,ncol=3,byrow=T,),1 ,2 ,3,1, 1 2 3,2, 4 5 6,3, 7 8 9, matrix(c(1:9),nrow=3,ncol=3),1 ,2 ,3,1, 1 4 7,2, 2 5 8,3, 3 6 9, bull=matrix(c(1:9),nrow=3,ncol=3,byrow=T),dim(bull),1 3 3,dimnames(bull),NULL,dimnames(bull)=list(c(A,B,C),c(D,E,F), bull,D E F,A 1 2 3,B 4 5 6,C 7 8 9, dimnames(bull),1,1 A B C,2,1 D E F,4,清单,(List),:与向量相对应,但元素类型不必相同。, blah=list(c(1,two,three,4),matrix(c(1,2,3,4,5,6),nrow,=2,ncol=3),c(1,7,17), blah,1,1 1 two three 4,2,1 ,2 ,3,1, 1 3 5,2, 2 4 6,3,1 1 7 17,引用:, blah1,哪个对象?,1 1 two three 4, blah13,哪个对象;哪个元素?,1 three, blah21:2,2,1 3 4,命名, names(blah)=c(some.characters,a.matrix,some.numbers), blah,$some.characters,1 1 two three 4,$a.matrix,1 ,2 ,3,1, 1 3 5,2, 2 4 6,$some.numbers,1 1 7 17,命名后的引用, blah $ some.characters4,1 4, blah14,1 4,5,数据框,(Data Frame),:多维。与矩阵相对应,但元素类型不要求相同。,6,类型检视与转换, blash,1 1 2 3 d NA, is.vector(blash),是向量?,1 TRUE, is.character(blash),是字符型?,1 TRUE, is.numeric(blash),是数值型?,1 FALSE,类型转换, as.numeric(1),1 1, as.numeric(hello),1 NA,Warning message:,强制改变过程中产生了,NA,as.vector(), as.matrix(), data.frame(),注意:,as.matrix(),能使一个向量变成,n1,阶的矩阵。,as.vector(),能使矩阵变成向量,(,从左至右一列列的读取,),。,第三章 统计分布及模拟,1,sample,函数:产生随机排列或随机样本。, sample(10),不放回,(,随机排列,),1 5 2 7 1 4 6 9 3 8 10, sample(10,replace=T),放回,1 10 4 8 3 3 9 1 9 6 4, sample(c(-1,0,1),size=20,prob=c(1/4,1/2,1/4),replace=T),1 0 -1 0 0 0 1 -1 1 1 0 0 0 -1 -1 -1 0 -1 1 0 0,设定随机种子, set.seed(12345), sample(10),1 8 10 7 9 3 1 2 4 6 5, sample(10),1 1 2 6 10 3 8 9 4 7 5,#,随机排列不同。, set.seed(12345), sample(10),1 8 10 7 9 3 1 2 4 6 5,#,和第一个结果相同。,模拟随机行走:,w(t+1)=w(t)+r(t); r(t)=1 or -1,概率都是,1/2,,最初位置,w(0)=0,。, set.seed(13579), r=sample(c(-1,1),size=100,replace=T,prob=c(1/2,1/2), r=c(0,r), r,1 0 1 -1 1 -1 1 1 1 -1 -1 1 -1 -1 -1 -1 -1 -1 1 -1 1 1 -1 -1 -1 1 1 1 1 -1 1 -1 1 -1 1 1 -1,37 -1 1 1 -1 1 1 -1 1 1 1 1 -1 1 -1 1 1 -1 1 1 -1 1 -1 1 1 -1 -1 -1 -1 1 -1 -1 1 -1 1 1 1,73 1 -1 -1 -1 -1 -1 1 -1 -1 -1 1 -1 1 -1 1 1 1 -1 1 -1 1 1 -1 1 1 1 -1 1 -1, w=cumsum(r), w,1 0 1 0 1 0 1 2 3 2 1 2 1 0 -1 -2 -3 -4 -3 -4 -3 -2 -3 -4 -5 -4 -3 -2 -1 -2 -1 -2 -1 -2 -1 0 -1,37 -2 -1 0 -1 0 1 0 1 2 3 4 3 4 3 4 5 4 5 6 5 6 5 6 7 6 5 4 3 4 3 2 3 2 3 4 5,73 6 5 4 3 2 1 2 1 0 -1 0 -1 0 -1 0 1 2 1 2 1 2 3 2 3 4 5 4 5 4, w=as.ts(w), plot(w,main=Random Walk), abline(h=0),2,统计分布,四个基本项目:,(1),概率密度函数,d,(2),累计分布函数,p,(3),分位数,q,(4),伪随机数,r,例:绘制标准正态分布的概率密度函数。, x=seq(-4,4,0.1), plot(x,dnorm(x),type=l,main=N(0,1) Density),例:计算标准正态随机变量的,Z pnorm(1.96),1 0.9750021,例:,T t(5),,计算,T2.3,的概率。, 1-pt(2.3,5),1 0.03488623, pt(2.3,5,lower.tail=F) #,右尾概率,1 0.03488623,例:计算标准正态分布的分位数。, q=c(0.025,0.05,0.5,0.95,0.975), qnorm(q),1 -1.959964 -1.644854 0.000000 1.644854 1.959964,例:产生,1000,个,N(10,4),的随机数据,绘制直方图。, hist(rnorm(1000,mean=10,sd=2),main=N(10,4),例:绘制,n=20,,,p=1/4,的二项式分布的概率密度函数。, x=seq(0:20), pmf=dbinom(x,size=20,prob=1/4), plot(x,pmf,type=h,main=Binomial(20,1/4),分布,),例:计算,Binomial(20,1/4),随机变量,X8,的概率。, 1-pbinom(8,size=20,prob=1/4),1 0.04092517,例:计算,Binomial(20,1/4),的分位数。, q=seq(0.1,0.9,0.1), qbinom(q,size=20,prob=1/4),1 3 3 4 4 5 5 6 7 8,3,中心极限定理,产生,1000,个独立的均匀分布,uniform(0,1),随机数据。, set.seed(12345), u=runif(10000), summary(u),Min. 1st Qu. Median Mean 3rd Qu. Max.,5.187e-05 2.537e-01 5.046e-01 5.007e-01 7.492e-01 1.000e+00, var(u),1 0.08240007, hist(u, main=U(0,1),将,u,转成一个,1000,的矩阵,计算每一行的样本平均值,这个的抽样分布。, u=matrix(u,nc=10), m=apply(u,1,mean), summary(m),Min. 1st Qu. Median Mean 3rd Qu. Max.,0.2620 0.4405 0.4982 0.5665 0.8304, var(m) 0.5007,1 0.007678896, hist(m,main=,样本平均,(n=10),的抽样分布,),第四章 程序编写,1,函数的编写,my.function=function(arg1,arg2),+,+ ,+ value.to.return,+ ,my.function,为函数名;,arg1,arg2,为参数;最后一行程序代码所代表的数值会被传回,这就是,value.to.return,的数值。,具体例子,:, stdev=function(x),+ ,+ sqrt(var(x),+ , z=c(1,2,0.45,-1.2,2.3), stdev(z),1 1.395708,函数内的变量赋值只在函数内有效。, test=unchanged, changer=function(),+ ,+ test=changed,+ test,+ , changer(),1 changed, test,1 unchanged,若想利用函数,changer,去变更,test,的值,可用以下做法:, test,1 unchanged, test=changer(), test,1 changed,编写程序可用,#,来引导注释。,如果使用者只输入函数的名称,,R,会显示该函数的定义。, changer,function(),test=changed,test,2,函数的编辑,fix(stdev),3,循环和逻辑,for,语句,for(,指标变量,in,执行范围,),例子:, total=0, for(i in 1:10),+ ,+ total=total+i,+ , total,1 55,例子,:, total=0, for(i in c(1,2,4,5,9),+ ,+ total=total+i,+ , total,1 21,(2) if,语句,if (,条件式,),else,例子:, x=1, if (x max.power=function(x,y),+ ,+ if(x0),+ if(x1),+ i=1,+ total=x,+ while(total*x max.power(2,100),1 6, max.power(2,1),NULL, max.power(0.5,100),1 “Infinity”,4,Apply,函数,不用,Apply:, get.mean=function(x),+ ,+ report=rep(0,4),+ for(i in 1:4),+ reporti=mean(x,i),+ report,+ , data(iris), get.mean(iris),1 5.843333 3.057333 3.758000 1.199333,用,Apply,函数,:, get.mean(iris),1 5.843333 3.057333 3.758000 1.199333, apply(iris,1:4,2,mean),Sepal.Length Sepal.Width Petal.Length Petal.Width,5.843333 3.057333 3.758000 1.199333,第五章 统计模型,1,回归模型,lm(,因变量,自变量,+,自变量,+-1), data(faithful), attach(faithful), names(faithful),1 eruptions waiting, plot(eruptions,waiting), lm(waitingeruptions),Call:,lm(formula = waiting eruptions),Coefficients:,(Intercept) eruptions,33.47 10.73, lm(waitingeruptions,-1,),Call:,lm(formula = waiting eruptions - 1),Coefficients:,eruptions,19.40, faithful.lm=lm(waitingeruptions), summary(faithful.lm),Call:,lm(formula = waiting eruptions),Residuals:,Min 1Q Median 3Q Max,-12.0796 -4.4831 0.2122 3.9246 15.9719,Coefficients:,Estimate Std. Error t value Pr(|t|),(Intercept) 33.4744 1.1549 28.98 2e-16 *,eruptions 10.7296 0.3148 34.09 2e-16 *,-,Signif. codes: 0 * 0.001 * 0.01 * 0.05 . 0.1 1,Residual standard error: 5.914 on 270 degrees of freedom,Multiple R-squared: 0.8115, Adjusted R-squared: 0.8108,F-statistic: 1162 on 1 and 270 DF,p-value: par(mfrow=c(2,2), plot(faithful.lm), lm(waitingeruptions,subset=c(-58),Call:,lm(formula = waiting eruptions, subset = c(-58),Coefficients:,(Intercept) eruptions,33.20 10.80,2,方差分析模型, data(PlantGrowth), attach(PlantGrowth), names(PlantGrowth),1 weight group“,将,group,转换成因子, group=as.factor(group),绘制盒形图, plot(group,weight,main=,植物重量,xlab=,肥料,), summary(aov(weightgroup),Df Sum Sq Mean Sq F value Pr(F),group 2 3.7663 1.8832 4.8461,0.01591,*,Residuals 27 10.4921 0.3886,-,Signif. codes: 0 * 0.001 * 0.01 * 0.05 . 0.1 1,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 小学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!