第十讲R语言中的统计.ppt_第1页
第十讲R语言中的统计.ppt_第2页
第十讲R语言中的统计.ppt_第3页
第十讲R语言中的统计.ppt_第4页
第十讲R语言中的统计.ppt_第5页
免费预览已结束,剩余37页可下载查看

下载本文档

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

文档简介

1、第十讲 R语言中的统计,一、R语言中的概率,分布函数的形式rfunc(n,p1,p2,.),其中func指概率分布函数,n为生成数据的个数,p1, p2, . . . 是分布的参数数值。 如:rnorm #随机产生正态分布的数据 rnorm(100,2,5) #mean=1,sd=5 随机分布函数rfunc r:random 密度函数dfunc d:density 累计概率密度函数pfunc p:probability 分位数函数qfunc q:quantile,概率分布 R 对应的名字 附加参数 分布 beta shape1, shape2, ncp 二项式分布 binom size, pr

2、ob Cauchy 分布 cauchy location, scale 卡方分布 chisq df, ncp 指数分布 exp rate F分布 f df1, df1, ncp 分布 gamma shape, scale 几何分布 geom prob 超几何分布 hyper m, n, k 对数正态分布 lnorm meanlog, sdlog logistic 分布 logis location, scale 负二项式分布 nbinom size, prob 正态分布 norm mean, sd Poisson 分布 pois lambda t 分布 t df, ncp 均匀分布 unif

3、min, max Weibull 分布 weibull shape, scale Wilcoxon 分布 wilcox m, n,二项分布,rbinom(n,size,prob) n为产生的随机数的个数(可以大于szie),prob为单点分 布的成功的概率。size为二项分布的试验次数., rbinom(5,10,0.5) 1 7 4 7 5 5 rbinom(5,10,0.1) 1 1 2 3 0 1,概率密度函数,在n次试验中事件A发生x次的概率p(x),p为单次试验事件A发 生的概率公式:,设100次试验,A发生的概率为0.3,A发生20次的概率为:, dbinom(20, 100, 0

4、.3) 1 0.007575645,A发生2060次的概率: sum(dbinom(20:60, 100, 0.3) 1 0.9911128,累积概率密度函数及图, pbinom(60,100,0.5)-pbinom(39,100,0.5) 1 0.9647998频率0.5,100次中发生39 到60次概率 pbinom(6,10,0.5)-pbinom(3,10,0.5) 1 0.65625, sum(dbinom(40:60, 100, 0.5) 1 0.9647998 sum(dbinom(4:6, 10, 0.5) 1 0.65625, plot(dbinom(0:100,100,0.

5、5) plot(pbinom(0:100,100,0.5),指定累积概率的q值, qbinom(p=0.5,size=10,prob=0.2) 1 2,求成功概率为0.2,总次数为10,指定累积概率为0.5的试验次数:, pbinom(q=2,size=10,prob=0.2) 1 0.6777995 pbinom(q=1,size=10,prob=0.2) 1 0.3758096 pbinom(q=3,size=10,prob=0.2) 1 0.8791261,检验,泊松分布,rpois(n,lambda) ppois(q, lambda, lower.tail = TRUE, log.p

6、= FALSE) dpois(x, lambda, log = FALSE),概率密度函数 x=1:10 dpois(x,3) 1 0.1493612051 0.2240418077 0.2240418077 0.1680313557 0.1008188134 6 0.0504094067 0.0216040315 0.0081015118 0.0027005039 0.0008101512 累积概率密度函数 ppois(x,3) 1 0.1991483 0.4231901 0.6472319 0.8152632 0.9160821 0.9664915 0.9880955 8 0.996197

7、0 0.9988975 0.9997077,p(x) = x exp(-)/x!,指定累积概率的q值 qpois(p, lambda, lower.tail = TRUE, log.p = FALSE), x=seq(0,1,0.1) x 1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 qpois(x,3) 1 0 1 2 2 2 3 3 4 4 5 Inf,正态分布,产生正态分布随机数 rnorm(n, mean=0, sd=1), rnorm(10,0,1) 1 0.9944192 -0.1384374 -0.8876501 1.0416947

8、-0.3217919 -0.8546145 7 -2.0329649 -0.5276146 0.1380986 -0.8563042,密度-累积概率密度函数, x plot(dnorm(x) plot(pnorm(x),指定累积概率的q值,qnorm(p, mean=0, sd=1, lower.tail = TRUE, log.p = FALSE) x=seq(0,1,0.1) qnorm(x) 1 -Inf -1.2815516 -0.8416212 -0.5244005 -0.2533471 0.0000000 7 0.2533471 0.5244005 0.8416212 1.2815

9、516 Inf,t分布, rt(n=10,df=5) 1 0.7965116 0.9019405 0.2392244 0.3129466 -0.2910085 -1.2970800 7 1.4356046 0.1165443 0.9069540 0.3450907,密度-累积概率密度函数, xplot(x,dt(x,df=20) plot(x,pt(x,df=20),指定累积概率的q值, qt(p=0.025,df=20) 1 -2.085963 qt(p=0.975,df=20) 1 2.085963,画出自由度为4,6,8,10,20,30的t分布密度函数图形并在一张图上和标准正态分布密度

10、函数图形作比较,你能得出什么结论?,练习10.1,二、描述性统计, x=exp(seq(-1,3,by=0.1) x 1 0.3678794 0.4065697 0.4493290 0.4965853 0.5488116 0.6065307 7 0.6703200 0.7408182 0.8187308 0.9048374 1.0000000 1.1051709 13 1.2214028 1.3498588 1.4918247 1.6487213 1.8221188 2.0137527 19 2.2255409 2.4596031 2.7182818 3.0041660 3.3201169 3

11、.6692967 25 4.0552000 4.4816891 4.9530324 5.4739474 6.0496475 6.6858944 31 7.3890561 8.1661699 9.0250135 9.9741825 11.0231764 12.1824940 37 13.4637380 14.8797317 16.4446468 18.1741454 20.0855369 plot(x),样本均值, mean(x) 1 5.062624,样本方差, var(x) 1 29.35325,标准差,s=, sd(x) 1 5.417864,colMeans(y) # 行均值,rowMe

12、ans(y) # 列均值,最大最小值, max(x) 1 20.08554 min(x) 1 0.3678794,累积最大最小值, cummax(x) 1 0.3678794 0.4065697 0.4493290 0.4965853 0.5488116 0.6065307 7 0.6703200 0.7408182 0.8187308 0.9048374 1.0000000 1.1051709 13 1.2214028 1.3498588 1.4918247 1.6487213 1.8221188 2.0137527 19 2.2255409 2.4596031 2.7182818 3.00

13、41660 3.3201169 3.6692967 25 4.0552000 4.4816891 4.9530324 5.4739474 6.0496475 6.6858944 31 7.3890561 8.1661699 9.0250135 9.9741825 11.0231764 12.1824940 37 13.4637380 14.8797317 16.4446468 18.1741454 20.0855369 cummin(x) 1 0.3678794 0.3678794 0.3678794 0.3678794 0.3678794 0.3678794 0.3678794 8 0.36

14、78794 0.3678794 0.3678794 0.3678794 0.3678794 0.3678794 0.3678794 15 0.3678794 0.3678794 0.3678794 0.3678794 0.3678794 0.3678794 0.3678794 22 0.3678794 0.3678794 0.3678794 0.3678794 0.3678794 0.3678794 0.3678794 29 0.3678794 0.3678794 0.3678794 0.3678794 0.3678794 0.3678794 0.3678794 36 0.3678794 0.

15、3678794 0.3678794 0.3678794 0.3678794 0.3678794,差分, diff(x) 1 0.03869022 0.04275930 0.04725634 0.05222633 0.05771902 0.06378939 7 0.07049817 0.07791253 0.08610666 0.09516258 0.10517092 0.11623184 13 0.12845605 0.14196589 0.15689657 0.17339753 0.19163391 0.21178822 19 0.23406218 0.25867872 0.28588420

16、 0.31595090 0.34917974 0.38590330 25 0.42648910 0.47134335 0.52091497 0.57570007 0.63624698 0.70316166 31 0.77711381 0.85884359 0.94916896 1.04899393 1.15931758 1.28124407 37 1.41599369 1.56491505 1.72949860 1.91139155 plot(x,diff(x),中位数, median(x) 1 2.718282,众数(频数), y=c(1,1,2,2,2,3,4)*2 y 1 2 2 4 4

17、 4 6 8 table(y) y 2 4 6 8 2 3 1 1,变异系数(coecient of variability),CV-function(x) sd(x)/mean(x) , CV(x) 1 1.070169 失,分位数,最小的值为0,最大的为1,%u为min+(max-min)*%u, quantile(x) 0% 25% 50% 75% 100% 0.3678794 1.0000000 2.7182818 7.3890561 20.0855369 quantile(x,.25) 25% 1 quantile(x,c(.25,.75) 25% 75% 1.000000 7.38

18、9056,直方图, x=scan() 1: 29.6 28.2 19.6 13.7 13.0 7.8 3.4 2.0 1.9 1.0 0.7 0.4 0.4 0.3 15: 0.3 0.3 0.3 0.3 0.2 0.2 0.2 0.1 0.1 0.1 0.1 0.1 27: Read 26 items a=hist(x) # 频率,# 获得额外信息-频数、频率、组值、组限、中值等, str(a) List of 7 $ breaks : num 1:13 -3 -2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 . $ counts : int 1:12 0 1 1 1 3 4

19、 6 2 2 0 . $ intensities: num 1:12 0.0 0.1 0.1 0.1 0.3 . $ density : num 1:12 0.0 0.1 0.1 0.1 0.3 . $ mids : num 1:12 -2.75 -2.25 -1.75 -1.25 -0.75 -0.25 0.25 0.75 1.25 1.75 . $ xname : chr x,三、假设检验,检验数据是否来自某个分布(Kolmogorov-Smirnov Test), x y ks.test(x, y) Two-sample Kolmogorov-Smirnov test data: x a

20、nd y D = 0.48, p-value = 0.0002033 alternative hypothesis: two.sided,# x 是否来自正态分布 x ks.test(x, pnorm, 0, 1) One-sample Kolmogorov-Smirnov test data: x D = 0.0634, p-value = 0.816 alternative hypothesis: two.sided,正态性检验(ShapiroWilk test), shapiro.test(rnorm(100, mean = 5, sd = 3) Shapiro-Wilk normali

21、ty test data: rnorm(100, mean = 5, sd = 3) W = 0.9902, p-value = 0.6805, shapiro.test(rnorm(10000, mean = 5, sd = 3) 错误在shapiro.test(rnorm(10000, mean = 5, sd = 3) : 样本大小必需在3和5000之间,单样本假设检验,方差未知的正态分布均值的单样本检验(t检验), x=rnorm(200) t.test(x) One Sample t-test data: x t = -1.1695, df = 199, p-value = 0.24

22、36 alternative hypothesis: true mean is not equal to 0 95 percent confidence interval:置信区间 -0.21865305 0.05585082 sample estimates: mean of x -0.08140112,方差已知的正态分布均值的单样本检验(z检验), x=rnorm(100) z=(mean(x)-0)/(1/sqrt(100) z 1 2.005832 pnorm(z) 1 0.9775629,方差齐性检验-F检验, x y var.test(x, y) F test to compare

23、 two variances data: x and y F = 2.7983, num df = 49, denom df = 29, p-value = 0.004024 alternative hypothesis: true ratio of variances is not equal to 1 95 percent confidence interval:置信区间 1.405945 5.264822 sample estimates: ratio of variances方差比值 2.798329,四、回归,1、统计上常用回归的方法探讨两个或多个变量之间的关系。 2、回归分析的目的

24、是揭示出呈因果关系的相关变量间的联系形式,建立回归方程,利用所建立的回归方程由自变量来预测因变量。 3、回归问题分为一元回归、多元线性回归与非线性回归,其牵涉到的问题为模型的选择及回归精度的评价。,回归函数: lm(formula, data, subset, weights, na.action, method = qr, model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, contrasts = NULL, offset, .), car.1 car.1 Call: lm(formula = dist spe

25、ed, data = cars) Coefficients: (Intercept) speed -17.579 3.932 plot(car.1) plot(cars) abline(car.1),公式的典型形式是ymodel,其中y是响应变量,model是一些元素项的集合,而且要为其中一些项估计参数。这些元素项通过一些有特殊含义的运算符连接。 R公式里面采用的运算符和表达式里面使用的运算符有着不同的含义。例如:,回归模型的公式表示, summary(lm(distspeed,data=cars) Call: lm(formula = dist speed, data = cars) Res

26、iduals: Min 1Q Median 3Q Max -29.069 -9.525 -2.272 9.215 43.201 Coefficients: Estimate Std. Error t value Pr(|t|) (Intercept) -17.5791 6.7584 -2.601 0.0123 * speed 3.9324 0.4155 9.464 1.49e-12 * - Signif. codes: 0 * 0.001 * 0.01 * 0.05 . 0.1 1 Residual standard error: 15.38 on 48 degrees of freedom

27、# 残差的标准误 Multiple R-squared: 0.6511, Adjusted R-squared: 0.6438 # 相关系数的平方 F-statistic: 89.57 on 1 and 48 DF, p-value: 1.490e-12 # F值, 整体回归的检验,回归模型的详细信息,总平方和(SS总)=残差平方和(SS残)+回归平方和(SS回),残差的标准误,相关系数的平方,S=sqrt(SS残/n-2),R2=SS回/SS总,评价模型的适配性, car.2 summary(car.2) Call: lm(formula = dist 1 + speed + I(speed

28、2), data = cars) Residuals: Min 1Q Median 3Q Max -28.720 -9.184 -3.188 4.628 45.152 Coefficients: Estimate Std. Error t value Pr(|t|) (Intercept) 2.47014 14.81716 0.167 0.868 speed 0.91329 2.03422 0.449 0.656 I(speed2) 0.09996 0.06597 1.515 0.136 Residual standard error: 15.18 on 47 degrees of freed

29、om Multiple R-squared: 0.6673, Adjusted R-squared: 0.6532 F-statistic: 47.14 on 2 and 47 DF, p-value: 5.852e-12, anova(car.2) Analysis of Variance Table Response: dist Df Sum Sq Mean Sq F value Pr(F) speed 1 21185.5 21185.5 91.986 1.211e-12 * I(speed2) 1 528.8 528.8 2.296 0.1364 Residuals 47 10824.7 230.3 - Signif. codes: 0 * 0.001 * 0.01 * 0.05 . 0.1 1,五、广义线性模型,glm(formula, family = gaussian, data, weights, subset, na.action, start = NULL, etastart, mustart, offset, control = li

温馨提示

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

评论

0/150

提交评论