tft每日頭條

 > 科技

 > r語言逐步回歸分析法步驟

r語言逐步回歸分析法步驟

科技 更新时间:2024-07-26 19:22:45

發現大家做分析做的最多的還是線性回歸,很多人咨詢的都是線性回歸的問題,今天專門出一個線性回歸的文章。

在R語言中我們可以非常方便地用基礎包中的lm方法做出線性回歸。參數的書寫也和數學方程一樣一樣的Y~X X2,隻不過将等号換成了~。我們用summary 回歸對象就可以得到回歸結果,如果要看模型的殘差直接$resid就可以。

還是給大家寫一個活生生的例子吧:

實例描述:

我們有如圖的數據集,我想要用回歸分析做month, spend對sales的關系。

r語言逐步回歸分析法步驟(一般線性回歸的做法和解釋)1

首先讀入數據集(請私信“數據鍊接”獲取)并且規定數據屬性:

dataset = read.csv("data-marketing-budget-12mo.csv", header=T, colClasses = c("numeric", "numeric", "numeric"))

簡單線性回歸和多元線性回歸

在我們的例子中,因變量是sales,如果我隻用一個自變量,比如spend來做預測,此時就是簡單線性回歸;如果我用兩個或者兩個以上的自變量來做預測就是多元線性回歸,做法都很簡單:

simple.fit = lm(Sales~Spend, data=dataset) summary(simple.fit) multi.fit = lm(Sales~Spend Month, data=dataset) summary(multi.fit)

兩個模型的輸出都給大家貼在下面:

r語言逐步回歸分析法步驟(一般線性回歸的做法和解釋)2

對于模型,首先我們應該看整個模型的顯著性,也就是模型的F檢驗,可以看到兩個模型都有意義,然後我們再看R方和調整的R方,可以看到我們的模型賊好,然後我們再看每個變量的顯著性。

輸出結果的解釋

首先有一個residuals:

  • Residuals: The section summarizes the residuals, the error between the prediction of the model and the actual results. Smaller residuals are better.

這個是模型的殘差,就是模型預測值和實際值之間的差異,應該是越小越好。

接着就是coefficients:

  • Coefficients: For each variable and the intercept, a weight is produced and that weight has other attributes like the standard error, a t-test value and significance.

這個是模型中自變量的系數,這個系數又包含4個部分,分别是estimate,std,t和p

  • Estimate: This is the weight given to the variable. In the simple regression case (one variable plus the intercept), for every one dollar increase in Spend, the model predicts an increase of $10.6222.

estimate解釋為相應的自變量改變一個單位,應變量的改變量。Std. Error為它的标準誤,t value為檢驗系數顯著性的t統計量,Pr(>|t|)為p值,通過Pr(>|t|)我們可以知道該系數是不是顯著地不等于0。

還有模型整體表現的指标:

  • Residual Standard Error: This is the standard deviation of the residuals. Smaller is better.

這個是殘差的變異,越小越好。

  • Multiple / Adjusted R-Square: For one variable, the distinction doesn’t really matter. R-squared shows the amount of variance explained by the model. Adjusted R-Square takes into account the number of variables and is most useful for multiple-regression.

然後是R方和調整的R方,R方為這個模型能解釋的變異比例,調整的R方考慮了自變量個數。如果我們做簡單線性回歸的話R方和調整的R方就是一樣的。

還有模型表現的F-Statistic:

  • F-Statistic: The F-test checks if at least one variable’s weight is significantly different than zero. This is a global test to help asses a model. If the p-value is not significant (e.g. greater than 0.05) than your model is essentially not doing anything.

F統計量是來看整個模型是不是有意義的,如果模型整體沒意義相應的别的系數也就不用看了。

殘差相關知識

對于線性模型我們有四個假設:

  • The mean of the errors is zero (and the sum of the errors is zero)(線性)
  • The distribution of the errors are normal.(正态)
  • All of the errors are independent.(獨立)
  • Variance of errors is constant (Homoscedastic)(方差齊)

我們的模型滿不滿足這4個假設呢?我先畫圖看看:

layout(matrix(c(1,1,2,3),2,2,byrow=T)) plot(simple.fit$resid~dataset$Spend[order(dataset$Spend)], main=" 簡單線性回歸的自變量和殘差變化", xlab="Marketing Spend", ylab="Residuals") abline(h=0,lty=2) hist(simple.fit$resid, main="殘差的直方圖", ylab="Residuals") qqnorm(simple.fit$resid) qqline(simple.fit$resid)

r語言逐步回歸分析法步驟(一般線性回歸的做法和解釋)3

殘差是否正态?

我們可以從兩個圖中來判斷殘差是否正态:

  • If the histogram looks like a bell-curve it might be normally distributed.
  • If the QQ-plot has the vast majority of points on or very near the line, the residuals may be normally distributed.

首先是直方圖,直方圖是近似鐘形的就為正态,QQ圖中的點都和線靠得近就為正态。

但是我們數據量太少,看圖似乎看不出來,我們考慮做個統計檢驗:

library(fBasics) jarqueberaTest(simple.fit$resid)

r語言逐步回歸分析法步驟(一般線性回歸的做法和解釋)4

檢驗結果告訴我們殘差确實是正态的。

殘差是否獨立?

殘差獨立的意思就是殘差之間不存在相關性,我們也需要做統計檢驗:

library(lmtest) #dwtest dwtest(simple.fit)

r語言逐步回歸分析法步驟(一般線性回歸的做法和解釋)5

檢驗結果告訴我們殘差的自相關很大。

殘差是否齊?

對于這個假設,通常情況下我們也是看殘差圖,如果殘差圖沒有明顯的離群值我們就可以認為殘差是齊的。

小結

今天給大家比較詳細的寫了一般線性回歸和多元線性回歸的做法和解釋以及相應的模型評價。感謝大家耐心看完,自己的文章都寫的很細,代碼都在原文中,希望大家都可以自己做一做,請關注後私信回複“數據鍊接”獲取所有數據和本人收集的學習資料。如果對您有用請先收藏,再點贊轉發。

也歡迎大家的意見和建議。

如果你是一個大學本科生或研究生,如果你正在因為你的統計作業、數據分析、論文、報告、考試等發愁,如果你在使用SPSS,R,Python,Mplus, Excel中遇到任何問題,都可以聯系我。因為我可以給您提供好的,詳細和耐心的數據分析服務。

如果你對Z檢驗,t檢驗,方差分析,多元方差分析,回歸,卡方檢驗,相關,多水平模型,結構方程模型,中介調節,量表信效度等等統計技巧有任何問題,請私信我,獲取詳細和耐心的指導。

If you are a student and you are worried about you statistical #Assignments, #Data #Analysis, #Thesis, #reports, #composing, #Quizzes, Exams.. And if you are facing problem in #SPSS, #R-Programming, #Excel, Mplus, then contact me. Because I could provide you the best services for your Data Analysis.

Are you confused with statistical Techniques like z-test, t-test, ANOVA, MANOVA, Regression, Logistic Regression, Chi-Square, Correlation, Association, SEM, multilevel model, mediation and moderation etc. for your Data Analysis...??

Then Contact Me. I will solve your Problem...

加油吧,打工人!

猜你喜歡:

R數據分析:邏輯斯蒂回歸與泊松回歸

機器學習:邏輯回歸分類器(一)

R數據分析:多分類邏輯回歸

R數據分析:多元邏輯斯蒂回歸的做法

R數據分析:線性回歸的做法和優化實例

R數據分析:如何做邏輯斯蒂回歸

python機器學習:線性回歸中的啞變量轉換

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关科技资讯推荐

热门科技资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved