今天是各类统计方法R语言实现的第五期,我们主要介绍简单线性回归和多项式回归。 基础知识什么是回归?回归分析指用一个或多个自变量来预测因变量的方法。 简而言之,就是用已知的变量预测未知的变量,比如临床模型构建,常常使用的是回归来构建。 在医学中,Elisa等多种实验的标准曲线绘制使用的就是回归的的方法。 回归能做什么?①挑选与因变量相关的自变量。 ②描述自变量与因变量的关系。 ③通过自变量预测因变量。 常见回归类型汇总
(引自R语言实战(第二版)) 未来我们将介绍其中大部分方法。 简单线性回归(最小二乘法)基础概念最小二乘法的目的:使得回归模型的预测值与真实值的差值最小,具体表现为残差平方和最小。 简单线性模型:回归模型包含一个自变量和一个因变量,且二者成线性。 使用最小二乘法构建简单线性回归模型的假设: ①正态性:对于固定自变量,因变量成正态分布。 ②独立性:因变量取值相互独立。 ③线性:自变量和因变量之间为线性相关。 ④同方差性:因变量方差不随自变量的水平不同而变化。 使用lm()构建回归模型常用符号与代码

(引自R语言实战) 数据输入使用的数据自变量为身高(英寸),因变量为体重(磅),首先构建简单线性模型 data(women) summary(women) ## height weight ## Min. :58.0 Min. :115.0 ## 1st Qu.:61.5 1st Qu.:124.5 ## Median :65.0 Median :135.0 ## Mean :65.0 Mean :136.7 ## 3rd Qu.:68.5 3rd Qu.:148.0 ## Max. :72.0 Max. :164.0
代码展示#模型拟合 fit<-lm(weight ~ height,data = women)
##展示模型 summary(fit) ## ## Call: ## lm(formula = weight ~ height, data = women) ## ## Residuals: ## Min 1Q Median 3Q Max ## -1.7333 -1.1333 -0.3833 0.7417 3.1167 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) -87.51667 5.93694 -14.74 1.71e-09 *** ## height 3.45000 0.09114 37.85 1.09e-14 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 1.525 on 13 degrees of freedom ## Multiple R-squared: 0.991, Adjusted R-squared: 0.9903 ## F-statistic: 1433 on 1 and 13 DF, p-value: 1.091e-14 ##利用模型计算预测值 fitted(fit) ## 1 2 3 4 5 6 7 8 ## 112.5833 116.0333 119.4833 122.9333 126.3833 129.8333 133.2833 136.7333 ## 9 10 11 12 13 14 15 ## 140.1833 143.6333 147.0833 150.5333 153.9833 157.4333 160.8833 #计算每一个预测值与实际值之间的差值,即残差 residuals(fit) ## 1 2 3 4 5 6 ## 2.41666667 0.96666667 0.51666667 0.06666667 -0.38333333 -0.83333333 ## 7 8 9 10 11 12 ## -1.28333333 -1.73333333 -1.18333333 -1.63333333 -1.08333333 -0.53333333 ## 13 14 15 ## 0.01666667 1.56666667 3.11666667 #可视化 plot(women$height,women$weight, xlab="Height (in inches)", ylab="Weight(in pounds)") abline(fit)
img结果解读: 构建的模型为Y=-87.51667+3.45X,由于身高不可能为0,因此截距项没有实际意义。回归系数p=1.09e-14,非常显著。表明身高每增高1英寸,体重将预期增加3.45磅。 R方为0.991,表示模型可以解释99.1%的方差。 从图形中可以看出可以用曲线提高预测精度,所以我们接下来尝试多项式回归。 多项式回归(最小二乘法)多项式回归模型的关系为n阶多项式,我们首先尝试二次项 #二次项模型拟合 fit2<-lm(weight ~ height + I(height^2),data = women)
##展示模型 summary(fit2) ## ## Call: ## lm(formula = weight ~ height + I(height^2), data = women) ## ## Residuals: ## Min 1Q Median 3Q Max ## -0.50941 -0.29611 -0.00941 0.28615 0.59706 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 261.87818 25.19677 10.393 2.36e-07 *** ## height -7.34832 0.77769 -9.449 6.58e-07 *** ## I(height^2) 0.08306 0.00598 13.891 9.32e-09 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 0.3841 on 12 degrees of freedom ## Multiple R-squared: 0.9995, Adjusted R-squared: 0.9994 ## F-statistic: 1.139e+04 on 2 and 12 DF, p-value: < 2.2e-16 ##利用模型计算预测值 fitted(fit2) ## 1 2 3 4 5 6 7 8 ## 115.1029 117.4731 120.0094 122.7118 125.5804 128.6151 131.8159 135.1828 ## 9 10 11 12 13 14 15 ## 138.7159 142.4151 146.2804 150.3118 154.5094 158.8731 163.4029 #计算每一个预测值与实际值之间的差值,即残差 residuals(fit2) ## 1 2 3 4 5 6 ## -0.102941176 -0.473109244 -0.009405301 0.288170653 0.419618617 0.384938591 ## 7 8 9 10 11 12 ## 0.184130575 -0.182805430 0.284130575 -0.415061409 -0.280381383 -0.311829347 ## 13 14 15 ## -0.509405301 0.126890756 0.597058824 #可视化
plot(women$height,women$weight, xlab="Height(in inches)", ylab="Weight(in lbs)") lines(women$height,fitted(fit2))
img可以看到模型方差解释度为 0.9995,二次项显著,表明对于该数据多项式回归拟合效果更好。 #二次项模型拟合 fit3<-lm(weight ~ height + I(height^2)+ I(height^3),data = women)
##展示模型 summary(fit3) ## ## Call: ## lm(formula = weight ~ height + I(height^2) + I(height^3), data = women) ## ## Residuals: ## Min 1Q Median 3Q Max ## -0.40677 -0.17391 0.03091 0.12051 0.42191 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) -8.967e+02 2.946e+02 -3.044 0.01116 * ## height 4.641e+01 1.366e+01 3.399 0.00594 ** ## I(height^2) -7.462e-01 2.105e-01 -3.544 0.00460 ** ## I(height^3) 4.253e-03 1.079e-03 3.940 0.00231 ** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 0.2583 on 11 degrees of freedom ## Multiple R-squared: 0.9998, Adjusted R-squared: 0.9997 ## F-statistic: 1.679e+04 on 3 and 11 DF, p-value: < 2.2e-16 ##利用模型计算预测值 fitted(fit2) ## 1 2 3 4 5 6 7 8 ## 115.1029 117.4731 120.0094 122.7118 125.5804 128.6151 131.8159 135.1828 ## 9 10 11 12 13 14 15 ## 138.7159 142.4151 146.2804 150.3118 154.5094 158.8731 163.4029 #计算每一个预测值与实际值之间的差值,即残差 residuals(fit3) ## 1 2 3 4 5 6 ## 0.361437908 -0.406769374 -0.188012641 -0.007807225 0.108331538 0.134888314 ## 7 8 9 10 11 12 ## 0.046347770 -0.182805430 0.421913381 -0.165011133 0.030905696 -0.015851469 ## 13 14 15 ## -0.330797960 0.060550887 0.132679739 #可视化
plot(women$height,women$weight, xlab="Height(in inches)", ylab="Weight(in lbs)") lines(women$height,fitted(fit3))
img可以看到模型方差解释度为 0.9998,三次项显著,效果也不错。 好了,今天的R语言实现统计方法系列推文暂时告一段落,我们下次再见吧!小伙伴们如果有什么统计上的问题,或者如果想要学习什么方面的生物信息内容,可以在微信群或者知识星球提问,没准哪天的推文就是专门解答你的问题哦!
|