内容发布更新时间 : 2024/11/13 8:20:38星期一 下面是文章的全部内容请认真阅读。
精品文档
已知家兔体温服从正态分布,问注射前后体温有无显著差异?(α=0.01)(配对) 2. 使用t.text函数完成下面的例题
有甲、乙两台机床加工相同的产品,假定两台机床加工的产品均服从正态分布,且总体方差相等,从这两台机床加工的产品中随机地抽取若干件,测得产品直径(mm)为甲:(20.5,19.8,19.7,20.4,20.1,20.0,19.0,19.9);乙:(19.7,20.8,20.5,19.8,19.4,20.6,19.2)试比较甲乙两台机床加工的产品直径有无显著应差异?.(α=0.05)
1. 答案代码
x <- c(37.8,38.2,38.0,37.6,37.9,38.1,38.2,37.5,38.5,37.9) y <- c(37.9,39.0,38.9,38.4,37.9,39.0,39.5,38.6,38.8,39.0) α <- 0.01
solution <- t.test(x,y,mu = 0,alternative=\α)
if(solution$p.value>α){ print(\接受H0\}else{
print(\拒绝H0,接受H1\}
输出结果:拒绝H0,接受H1(即:注射前后体温差异显著) 2
答案代码
x <- c(20.5,19.8,19.7,20.4,20.1,20.0,19.0,19.9) y <- c(19.7,20.8,20.5,19.8,19.4,20.6,19.2)
精品文档
精品文档
α <- 0.05
solution <- t.test(x,y,mu = 0,alternative=\α)
if(solution$p.value>α){ print(\接受H0\}else{
print(\拒绝H0,接受H1\}
输入结果:接受H0(即:甲乙两台机床加工的产品直径无显著性差异)”
1:根据经验,在人的身高相等的情况下,血压的收缩Y与体重X1(千克)和年龄X2(岁数)有关,现收集了13个男子的数据,试建立Y关于X1、X2的线性回归方程。
第一步、录入数据,建立多元线性数据模型
X1<-c(76.0,91.5,85.5,82.5,79.0,80.5,74.5,79.0,85.0,76.5,82.0,95.0,92.5)
精品文档
精品文档
X2<-c(50,20,20,30,30,50,60,50,40,55,40,40,20)
Y<-c(120,141,124,126,117,125,123,125,132,123,132,155,147) blood<-data.frame(X1,X2,Y) lm.sol<-lm(Y~1+X1+X2,data=blood) summary(lm.sol) 得出Call:
lm(formula=Y~X1+X2,data=blood) Residuals: Min1QMedian3QMax
-4.0404-1.01830.46400.69084.3274 Coefficients:
EstimateStd.ErrortvaluePr(>|t|)
(Intercept)-62.9633616.99976-3.7040.004083** X12.136560.1753412.1852.53e-07*** X20.400220.083214.8100.000713*** ---
Signif.codes:
0‘***’0.001‘**’0.01‘*’0.05‘.’0.1‘’1 Residualstandarderror:2.854on10degreesoffreedom MultipleR-squared:0.9461,AdjustedR-squared:0.9354 F-statistic:87.84on2and10DF,p-value:4.531e-07
精品文档
精品文档
在我们的输入中,关键是lm.sol<-lm(Y~X1+X2+1,data=blood)的调用,这里可以看到,lm使用了参数Y~1+X1+X2,,即表示我们使用的是模型y=d+cx2+bx+e
从计算结果可以得到,回归系数与回归方程检验都是显著的,因此,回归方程为:
Y=-62.96+2.13X1+0.40X2
2: 一元线性回归:
x<-c(0.10, 0.11, 0.12, 0.13, 0.14, 0.15,0.16, 0.17, 0.18, 0.20, 0.21, 0.23)
y<-c(42.0, 43.5, 45.0, 45.5, 45.0, 47.5,49.0, 53.0, 50.0, 55.0, 55.0, 60.0) lm.sol<-lm(y ~ 1+x)
lm()函数返回拟合结果的对象,可以用summary()函数查看其内容。
summary(lm.sol) Residuals:
Min 1Q Median 3Q Max -2.0431 -0.7056 0.1694 0.6633 2.2653 Coefficients:
Estimate Std. Error t value Pr(>|t|)
精品文档
精品文档
(Intercept) 28.493 1.580 18.04 5.88e-09 ***
x 130.835 9.683 13.51 9.50e-08 ***
y=-28.49+130.84x
1、 删除异常点,重新拟定模型,数据如下(提示:residuals()函数绘图观察)
x <- c(194.5,194.3,197.9,198.4,199.4,199.9,200.9,201.1, 201.4,201.3,203.6,204.6,209.5,208.6,210.7,211.9,212.2) y <- c(131.79,131.79,135.02,135.55,136.46,136.83,137.82,138.00, 138.06,138.05,140.04,142.44,145.47,144.34,146.30,147.54,147.80) 答案
x <- c(194.5,194.3,197.9,198.4,199.4,199.9,200.9,201.1, 201.4,201.3,203.6,204.6,209.5,208.6,210.7,211.9,212.2) y <- c(131.79,131.79,135.02,135.55,136.46,136.83,137.82,138.00, 138.06,138.05,140.04,142.44,145.47,144.34,146.30,147.54,147.80) plot(x,y) #查看x和y之间的关系 lm.sol=lm(x~y) #拟合模型 summary(lm.sol)
lm.res <- residuals(lm.sol) #残差
plot(lm.res) #奇异点,可以看到第12位有明显问题 lm.up<- lm(x~y,subset = -12) summary(lm.up)
精品文档