当前位置:首页 > 统计建模与r软件第八章答案
172.3827 211.9173 sample estimates: mean of x 192.15
p值小于0.05,拒绝原假设,认为油漆工人的血小板计数与正常成年男子有差异。(原假设:油漆工人的血小板计数与正常成年男子无差异;备择假设:油漆工人的血小板计数与正常成年男子有差异。)
t.test(x,mu=225,alternative=less)##单边检验(备择假设) one sample t-test data: x
t = -3.4783, df = 19, p-value = 0.001258
alternative hypothesis: true mean is less than 225 95 percent confidence interval: -inf 208.4806
sample estimates: mean of x 192.15
结论:油漆工人的血小板计数小于正常成年男子。 5.2
pnorm(1000,mean(x),sd(x)) [1] 0.5087941 x
[1] 1067 919 1196 785 1126 936 918 1156 920 948 pnorm(1000,mean(x),sd(x)) [1] 0.5087941
结论:x=1000的概率为0.509, x大于1000的概率为0.491. 5.3
a-c(113,120,138,120,100,118,138,123) b-c(138,116,125,136,110,132,130,110) t.test(a,b,paired=true) paired t-test data: a and b
t = -0.6513, df = 7, p-value = 0.5357
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval: -15.628898.87889 sample estimates:
mean of the differences -3.375
p值大于0.05,接受原假设,即两种治疗方法无差异。 5.4
(1)正态性w检验:
x-c(-0.7,-5.6,2,2.8,0.7,3.5,4,5.8,7.1,-0.5,2.5,-1.6,1.7,3,0.4,4.5,4.6,2.5,6,-1.4)
y-c(3.7,6.5,5,5.2,0.8,0.2,0.6,3.4,6.6,-1.1,6,3.8,2,1.6,2,2.2,1.2,3.1,1.7,-2) shapiro.test(x) shapiro-wilk normality test data: x
w = 0.9699, p-value = 0.7527 shapiro.test(y)
shapiro-wilk normality test data: y
【篇二:统计建模与r软件课后习题答案2-5章】
用薛毅编的《统计建模与r软件》吧,找不出更好的了…… 工作环境仍是linux。 第二章答案: ex2.1 x-c(1,2,3)
y-c(4,5,6) e-c(1,1,1) z=2*x+y+e
z1=crossprod(x,y)#z1为x1与x2的内积 或者 x%*%y z2=tcrossprod(x,y)#z1为x1与x2的外积 或者 x%o%y z;z1;z2
要点:基本的列表赋值方法,内积和外积概念。内积为标量,外积为矩阵。 ex2.2
a-matrix(1:20,c(4,5));a b-matrix(1:20,ow=4,byrow=true);b c=a+b;c
#不存在ab这种写法 e=a*b;e
f-a[1:3,1:3];f
h-matrix(c(1,2,4,5),ow=1);h
#h起过渡作用,不规则的数组下标 g-b[,h];g
要点:矩阵赋值方法。默认是byrow=false,数据按列放置。
取出部分数据的方法。可以用数组作为数组的下标取出数组元素。 ex2.3 x-c(rep(1,times=5),rep(2,times=3),rep(3,times=4),rep(4,times=2));x #或者省略times=,如下面的形式
x-c(rep(1,5),rep(2,3),rep(3,4),rep(4,2));x
要点:rep()的使用方法。rep(a,b)即将a重复b次 ex2.4
n - 5; h-array(0,dim=c(n,n)) for (i in 1:n){for (j in 1:n){h[i,j]-1/(i+j-1)}};h
g - solve(h);g #求h的逆矩阵
ev - eigen(h);ev #求h的特征值和特征向量 要点:数组初始化;for循环的使用
待解决:如何将很长的命令(如for循环)用几行打出来再执行?每次想换行的时候一按回车就执行了还没打完的命令... ex2.5
studentdata-data.frame(name=c(zhangsan,lisi,wangwu,zhaoliu,din
gyi),sex=c(f,m,f,m,f),age=c(14,15,16,14,15),height=c(156,165,157,162,159),weight=c(42,49,41.5,52,45.5));studentdata 要点:数据框的使用
待解决:ssh登陆linux服务器中文显示乱码。此处用英文代替。 ex2.6
write.table(studentdata,file=studentdata.txt)
#把数据框studentdata在工作目录里输出,输出的文件名为studentdata.txt.
studentdata_a-read.table(studentdata.txt);studentdata_a #以数据框的形式读取文档studentdata.txt,存入数据框studentdata_a中。
write.csv(studentdata_a,studentdata.csv)
#把数据框studentdata_a在工作目录里输出,输出的文件名为studentdata.csv,可用excel打开. 要点:读写文件。read.table(file) write.table(rdata,file)
read.csv(file) write.csv(rdata,file)
外部文件,不论是待读入或是要写出的,命令中都得加双引号。 ex2.7
fun-function(n){
if(n = 0)
list(fail=please input a integer above 0!) else{ repeat{
if(n==1) break
else if(n%%2==0){n-n/2} else n- 3*n+1 } list(sucess!) }
} 在linux下新建一个r文件,输入上述代码,保存为2.7.r
然后在当前目录下进入r环境,输入source(2.7.r),即打开了这个程序脚本。
然后就可以执行函数了。 输入fun(67),显示 输入fun(-1),显示 $fail
[1] please input a integer above 0!
待解决:source(*.r)是可以理解为载入这个r文件吧?如何在r环境下关闭r文件呢?
ok,自己写的第一个r程序~~ 第二章答案: ex2.1
x-c(1,2,3) y-c(4,5,6) e-c(1,1,1) z=2*x+y+e
z1=crossprod(x,y)#z1为x1与x2的内积或者 x%*%y z2=tcrossprod(x,y)#z1为x1与x2的外积或者 x%o%y z;z1;z2
要点:基本的列表赋值方法,内积和外积概念。内积为标量,外积为矩阵。 ex2.2
a-matrix(1:20,c(4,5));a
b-matrix(1:20,ow=4,byrow=true);b c=a+b;c
#不存在ab这种写法 e=a*b;e
f-a[1:3,1:3];f
h-matrix(c(1,2,4,5),ow=1);h
共分享92篇相关文档