R语言学习详解

i123456789101112131415161718192021222324252627X15.683.796.024.854.606.054.907.083.854.654.594.297.976.196.135.716.406.065.096.135.785.436.507.9811.545.843.8427名糖尿病人的资料X2X3X41.904.538.21.647.326.93.566.9510.81.075.888.32.324.057.50.641.4213.68.5012.608.53.006.7511.52.1116.287.90.636.597.11.973.618.71.976.617.81.937.579.91.181.426.92.0610.3510.51.788.538.02.404.5310.33.6712.797.11.032.538.91.715.289.93.362.968.01.134.3111.36.213.4712.37.923.379.810.891.2010.50.928.616.41.206.459.6Y11.28.812.311.613.418.311.112.19.68.49.310.68.49.610.910.114.89.110.810.213.614.916.013.220.013.310.4 y<-c(11.2,8.8,12.3,11.6,13.4,18.3,11.1,12.1,9.6,8.4,9.3,10.6,8.4,9.6,10.9,10.1,14.8,9.1,10.8,10.2,13.6,14.9,16.0,13.2,20.0,13.3,10.4)

x1<-c(5.68,3.79,6.02,4.85,4.60,6.05,4.90,7.08,3.85,4.65,4.59,4.29,7.97,6.19,6.13,5.71,6.40,6.06,5.09,6.13,5.78,5.43,6.50,7.98,11.54,5.84,3.84)

x2<-c(1.90,1.64,3.56,1.07,2.32,0.64,8.50,3.00,2.11,0.63,1.97,1.97,1.93,1.18,2.06,1.78,2.40,3.67,1.03,1.71,3.36,1.13,6.21,7.92,10.89,0.92,1.20)

x3<-c(4.53,7.32,6.95,5.88,4.05,1.42,12.60,6.75,16.28,6.59,3.61,6.61,7.57,1.42,10.35,8.53,4.53,12.79,2.53,5.28,2.96,4.31,3.47,3.37,1.20,8.61,6.45)

x4<-c(8.2,6.9,10.8,8.3,7.5,13.6,8.5,11.5,7.9,7.1,8.7,7.8,9.9,6.9,10.5,8.0,10.3,7.1,8.9,9.9,8.0,11.3,12.3,9.8,10.5,6.4,9.6)

blood<-data.frame(y,x1,x2,x3,x4) #建立数据集 lm.reg<-lm(y~x1+x2+x3+x4,data=blood) #回归

summary(lm.reg) #提取回归结果 lm.step<-step(lm.reg) #逐步回归

summary(lm.step) #提取回归结果 y.res<-residuals(lm.step) #计算残差

print(y.res) #显示y.res的值 y.rst<-rstandard(lm.reg) #计算标准化残差

y.fit<-predict(lm.reg) #取预测值 op<-par(mfrow=c(1,2)) #图分成两半 plot(y.res~y.fit) #残差拟合图

plot(y.rst~y.fit) #标准化残差拟合图 par(op) #图空间复原

非参数假设检验

符号检验

sign.test<-function(x,m0,alpha=0.05,alter=\ p<-list() n<-length(x)

sign<-as.numeric(x>=m0) s<-sum(sign)

result<-binom.test(s,n,p=0.5,alternative=alter,conf.level=alpha) p$p.value=result$p.value }

insure<-c(4632,4728,5052,5064,5484,6972,7696,9048,14760,15013,18730,21240,22836,52788,67200) #输入索赔数据

p<-sign.test(insure,6064) #进行符号检验,检验其中位数是否为6064 p #求出p值

Wilcoxon符号检验

wilcox.test(insure,mu=6064,conf.int=T) #进行Wilcoxon符号检验,检验其中位数是否为6064

Chi方检验

1、检验一批数据是否服从一个已知分布

v<-c(35,16,15,17,17,19,11,16,30,24) #10种球在200个球中的获取数 chisq.test(v) #检验不同球种的球数是否相同 2、检验两组变量是否独立

吸烟与肺癌的关系吸烟不吸烟肺癌患者603对照组3211 compare<-matrix(c(60,32,3,11),nr=2,dimnames=list(c(\,\

chisq.test(compare,correct=T)

Chi方近似检验要求2维列联表中只有20%以下的格子的期望频数小于5。若发生,则R会给出警告, 这时应该使用Fisher精确检验.。

fisher.test(compare, alternative = \

Wilcoxon Mann-Whitney U检验

例 有糖尿病的和正常的老鼠重量为(单位:克)

糖尿病鼠:42, 44, 38, 52, 48, 46, 34, 44, 38;

正常老鼠:34, 43, 35, 33, 34, 26, 30, 31, 31, 27, 28, 27, 30, 37, 32. 检验这两组的体重是否有显著不同?

diabetes<-c(42,44,38,52,48,46,34,44,38) #输入糖尿病鼠体重

normal<-c(34,43,35,33,34,26,30,31,31,27,28,27,30,37,32) #输入正常鼠体重 wilcox.test(diabetes,normal,exact = FALSE, correct=FALSE) #Wilcoxon Mann-Whitney U检验

Ansari-Bradley检验

例 两个工人加工的零件尺寸(各10个)为(单位:mm):

工人A: 18.0, 17.1, 16.4, 16.9, 16.9, 16.7, 16.7, 17.2, 17.5, 16.9; 工人B: 17.0, 16.9, 17.0, 16.9, 17.2, 17.1, 16.8, 17.1, 17.1, 17.2. 这个结果能否说明两个工人的水平(加工精度)一致?

worker.a<-c(18.0,17.1,16.4,16.9,16.9,16.7,16.7,17.2,17.5,16.9) #输入工人A加工的零件尺寸

worker.b<-c(17.0,16.9,17.0,16.9,17.2,17.1,16.8,17.1,17.1,17.2) #输入工人B加工的零件尺寸

ansari.test(worker.a,worker.b) # Ansari-Bradley检验

联系客服:779662525#qq.com(#替换为@)