R語言線性回歸

1、導入數(shù)據(jù)

library(haven)
OAP <- read_sav("C:/Users/deng/Desktop/OAP.sav")
str(OAP)
## Classes 'tbl_df', 'tbl' and 'data.frame':    38 obs. of  3 variables:
##  $ NO : num  1 2 3 4 5 6 7 8 9 10 ...
##   ..- attr(*, "format.spss")= chr "F8.0"
##  $ DON: num  0 0 0 0 0 ...
##   ..- attr(*, "format.spss")= chr "F8.2"
##  $ OAP: num  14.15 11.13 7.25 5.19 4.15 ...
##   ..- attr(*, "format.spss")= chr "F8.2"
print(OAP) #查看數(shù)據(jù)
NO  DON     OAP
1   0       14.15
2   0       11.13
3   0       7.25
4   0       5.19
5   0       4.15
6   0       3.29
7   0       2.26
8   0       0.01
9   28.76   3.27
10  48.54   3.34
11  57.94   4.28
12  69.18   7.2
13  225.41  14.16
14  187.89  7.2
15  74.78   9.27
16  74.67   14.1
17  86.09   9.26
18  75.89   2.2
19  116.33  5.27
20  128.58  5.26
21  178.42  9.19
22  177.38  13.24
23  204.63  16.15
24  215.99  14.16
25  206.9   0.03
26  247.29  5.17
27  289.54  11.18
28  306.31  19.1
29  327.23  11.15
30  358.32  11.13
31  389.22  19.12
32  419.35  20.05
33  426.85  21.33
34  426.9   19.18
35  458.04  17.09
36  468.34  20.01
37  577.52  24.24
38  588.95  19.06

2、繪制OAP評分和DON含量的散點圖

library(ggplot2)
ggplot(OAP, aes(x = DON, y = OAP)) + 
  geom_point() + 
  theme_bw()
image.png

3、計算OAP和DON的直線相關(guān)系數(shù)

with(OAP, (cor(OAP, DON, method = 'pearson'))) 
## [1] 0.7863221

4、檢驗直線相關(guān)系數(shù)的統(tǒng)計學意義

with(OAP, cor.test(OAP, DON, method = 'pearson')) 
##  Pearson's product-moment correlation
## 
## data:  OAP and DON
## t = 7.6365, df = 36, p-value = 4.89e-09
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.6233270 0.8838328
## sample estimates:
##       cor 
## 0.7863221
-Pearson直線相關(guān)系數(shù)檢驗,p<0.05,表明OAP評分和DON含量存在直線相關(guān)關(guān)系;
-相關(guān)系數(shù)為0.7863221,說明OAP評分與DON含量呈正相關(guān)。

5、建立OAP評分關(guān)于DON含量的線性模型

fit.lm <- lm(OAP ~ DON, data = OAP)
fit.lm
## Call:
## lm(formula = OAP ~ DON, data = OAP)
## 
## Coefficients:
## (Intercept)          DON  
##     4.78563      0.02969

6、生成線性模型的統(tǒng)計量

summary(fit.lm)
 Call:
lm(formula = OAP ~ DON, data = OAP)

 Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.8995  -2.9493  -0.1378   2.7526   9.3644 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 4.785631   1.021456   4.685 3.92e-05 ***
## DON         0.029695   0.003889   7.636 4.89e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.197 on 36 degrees of freedom
## Multiple R-squared:  0.6183, Adjusted R-squared:  0.6077 
## F-statistic: 58.32 on 1 and 36 DF,  p-value: 4.89e-09
-截距和解釋變量DON的系數(shù),p值均小于0.05,截距和系數(shù)均有意義。
-截距為4.785631,意為當DON的含量為0時,OAP的評分為4.785631 。DON的系數(shù)為0.029695,表示當DON平均改變一個單位時,OAP評分平均改變0.029695。
-調(diào)整R2為 0.6077,表明DON含量解釋了OAP評分60.77%的變異,剩下的29.23%變異由其他因素影響。

7、生成方差分析表

anova(fit.lm)
## Analysis of Variance Table
## 
## Response: OAP
##           Df  Sum Sq Mean Sq F value   Pr(>F)    
## DON        1 1027.21 1027.21  58.316 4.89e-09 ***
## Residuals 36  634.13   17.61                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

8、畫出擬合直線、在圖中標出回歸方程和調(diào)整R2

library(ggpmisc)
ggplot(OAP, aes(x = DON, y = OAP)) + 
    geom_point() + 
    theme_bw() + 
    geom_smooth(method = 'lm', se = F, color = 'red') + 
    stat_poly_eq(aes(label = paste(..eq.label.., ..adj.rr.label.., sep = '~~~~')), formula = y ~ x, parse = T)
image.png
export::table2doc(fit.lm, add.rownames = T)
Rplot.png

參考:用ggplot2進行直線回歸并添加回歸方程和方差分析表

9、附spss結(jié)果

點圖.png

相關(guān)系數(shù)及其檢驗.png
直線回歸結(jié)果.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容