R语言(介绍)-文档资料

上传人:日度 文档编号:143727318 上传时间:2020-09-01 格式:PPT 页数:74 大小:842.50KB
返回 下载 相关 举报
R语言(介绍)-文档资料_第1页
第1页 / 共74页
R语言(介绍)-文档资料_第2页
第2页 / 共74页
R语言(介绍)-文档资料_第3页
第3页 / 共74页
R语言(介绍)-文档资料_第4页
第4页 / 共74页
R语言(介绍)-文档资料_第5页
第5页 / 共74页
点击查看更多>>
资源描述

《R语言(介绍)-文档资料》由会员分享,可在线阅读,更多相关《R语言(介绍)-文档资料(74页珍藏版)》请在金锄头文库上搜索。

1、R-语言/软件,R免费 R 资源公开(不是黑盒子,也不是吝啬鬼) R可以在UNIX, Linux, Windows和OS X运行. R 有优秀的内在帮助系统. R有优秀的画图功能 学生能够轻松地转到商业支持的 S-Plus程序(如果需要使用商业软件) R语言有一个强大的,容易学习的语法,有许多内在的统计函数.,R 是计算机编程语言. 类似于UNIX语言,C语言,Pascal,Gauss语言等. 对于熟练的编程者, 它将觉得该语言比其他语言更熟悉. 而对计算机初学者, 学习R语言使得学习下一步的其他编程不那么困难. 那些傻瓜软件(SAS,SPSS等)语言的语法则完全不同. 通过用户自编程序, R

2、语言很容易延伸和扩大. 它就是这样成长的.,R的缺点,不如S-Plus在编辑输出的画图那样好 没有商业支持 (但有网上支持) 需要编程, 不傻瓜.,使用编程软件容易吗?,以R软件为例. 非常好学,极易入门 基本功能简单,和写公式一样 能在一个小时学会基本运算语句 在一天内学会编写函数(子程序)和利用软件包. 往往困难的反而是统计内容本身,R的历史,S语言在1980年代后期在AT它是一个由志愿者组成的工作努力的国际团队.,点击CRAN得到一批镜像网站,下载R(http:/www.r-project.org/),点击镜像网站比如ctex,选择base,R里面有什么?,Packages (每个都有大

3、量数据和可以读写修改的函数/程序),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

4、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 r(t)=1 or -1 概率都是1/2,最初位置w(0)=0。 set.s

5、eed(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

6、 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(

7、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 例:计算标

8、准正态分布的分位数。 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(

9、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

10、-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 函数的

11、编写 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(

12、) 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,例子: to

13、tal=0 for(i in c(1,2,4,5,9) + + total=total+i + total 1 21,(2) if语句 if (条件式) else 例子: x=1 if (x1) x is less than one elsex is greater than or equal to one 1 x is greater than or equal to one,(3) while语句 While(条件式) max.power=function(x,y) + + if(x0) + if(x1) + i=1 + total=x + while(total*x max.power(

14、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 app

15、ly(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) Coeffi

16、cients: (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

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 电子/通信 > 数据通信与网络

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号