2015-05-11 154 views
4

我試圖將一些分類變量與R中分類變量

model <- lm(price ~ carat+cut+color+clarity) 
summary(model) 

答案直系模型線性模型是:

Call: 
lm(formula = price ~ carat + cut + color + clarity) 

Residuals: 
    Min  1Q Median  3Q  Max 
-11495.7 -688.5 -204.1 458.2 9305.3 

Coefficients: 
      Estimate Std. Error t value Pr(>|t|)  
(Intercept) -3696.818  47.948 -77.100 < 2e-16 *** 
carat  8843.877  40.885 216.311 < 2e-16 *** 
cut.L   755.474  68.378 11.049 < 2e-16 *** 
cut.Q  -349.587  60.432 -5.785 7.74e-09 *** 
cut.C   200.008  52.260 3.827 0.000131 *** 
cut^4   12.748  42.642 0.299 0.764994  
color.L  1905.109  61.050 31.206 < 2e-16 *** 
color.Q  -675.265  56.056 -12.046 < 2e-16 *** 
color.C  197.903  51.932 3.811 0.000140 *** 
color^4  71.054  46.940 1.514 0.130165  
color^5   2.867  44.586 0.064 0.948729  
color^6  50.531  40.771 1.239 0.215268  
clarity.L 4045.728 108.363 37.335 < 2e-16 *** 
clarity.Q -1545.178 102.668 -15.050 < 2e-16 *** 
clarity.C  999.911  88.301 11.324 < 2e-16 *** 
clarity^4 -665.130  66.212 -10.045 < 2e-16 *** 
clarity^5  920.987  55.012 16.742 < 2e-16 *** 
clarity^6 -712.168  52.346 -13.605 < 2e-16 *** 
clarity^7 1008.604  45.842 22.002 < 2e-16 *** 
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 1167 on 4639 degrees of freedom 
Multiple R-squared: 0.9162, Adjusted R-squared: 0.9159 
F-statistic: 2817 on 18 and 4639 DF, p-value: < 2.2e-16 

但我不明白爲什麼答案是與「.L,.Q,.C,^ 4,...」,有些是錯誤的,但我不知道什麼是錯的,我已經嘗試了每個變量的函數因子。

+0

檢查類別分類變量。這一定是因素。每個因素水平都會有一個係數。 – vagabond

+4

這是一個序數。 '?訂購' –

+0

我只是解決了我的問題謝謝 –

回答

7

您遇到了順序因子變量如何由迴歸函數處理,並且默認的一組對比度是直到n-1度的正交多項式對比度,其中n是該因子的水平數。要解釋這個結果並不容易,特別是如果沒有自然秩序的話。即使有,並且在這種情況下可能會出現這種情況,但您可能不希望默認排序(按照因子級別按字母順序排列),並且您可能不希望多項式對比度中的度數超過幾個。

在GGPLOT2的鑽石數據集的情況下,該因子水平的設置是否正確,但是當他們偶然發現大多數新手下令因素得到有序的層次,如「優秀」 <「廣交會」 <「好」 <「差」。 (失敗)

> levels(diamonds$cut) 
[1] "Fair"  "Good"  "Very Good" "Premium" "Ideal"  
> levels(diamonds$clarity) 
[1] "I1" "SI2" "SI1" "VS2" "VS1" "VVS2" "VVS1" "IF" 
> levels(diamonds$color) 
[1] "D" "E" "F" "G" "H" "I" "J" 

一個methid要使用命令因素時,他們已正確設置是隻包起來as.numeric,讓你潮流的線性測試。

> contrasts(diamonds$cut) <- contr.treatment(5) # Removes ordering 
> model <- lm(price ~ carat+cut+as.numeric(color)+as.numeric(clarity), diamonds) 
> summary(model) 

Call: 
lm(formula = price ~ carat + cut + as.numeric(color) + as.numeric(clarity), 
    data = diamonds) 

Residuals: 
    Min  1Q Median  3Q  Max 
-19130.3 -696.1 -176.8 556.9 9599.8 

Coefficients: 
        Estimate Std. Error t value Pr(>|t|)  
(Intercept)   -5189.460  36.577 -141.88 <2e-16 *** 
carat    8791.452  12.659 694.46 <2e-16 *** 
cut2     909.433  35.346 25.73 <2e-16 *** 
cut3     1129.518  32.772 34.47 <2e-16 *** 
cut4     1156.989  32.427 35.68 <2e-16 *** 
cut5     1264.128  32.160 39.31 <2e-16 *** 
as.numeric(color) -318.518  3.282 -97.05 <2e-16 *** 
as.numeric(clarity) 522.198  3.521 148.31 <2e-16 *** 
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 1227 on 53932 degrees of freedom 
Multiple R-squared: 0.9054, Adjusted R-squared: 0.9054 
F-statistic: 7.371e+04 on 7 and 53932 DF, p-value: < 2.2e-16 
+1

我不是你最後一條建議的粉絲。 – Roland

+0

同意在某些方面,至少如果我正確地想象你的不適的基礎。使用as.numeric作爲快速和骯髒的替代品,將多項式對比度限制爲僅線性度,對於不瞭解因素表示的樸素建模者來說會有危險。我保證會提供一個備用的答案,用「對比度」或「C」函數來「正確」執行。 –

3

由於@Roland沒有張貼了他,他認爲將是一個更好的方法(和我和他那種同意),我需要教育自己一個真正的統計學家會怎麼做這在我R.最終在@SvenHohenstein的帖子中找到了正確的SO編碼建議:How to properly set contrasts in R我喜歡使用as.numeric的原因是我知道如何解釋係數。係數是LHS結果或因變量水平上的一個水平差異的「效應」(記住工作'效應'並不意味着因果關係)。看看我目前的第一個答案,在這個之上,你會發現cut2-5的係數值大約爲1000,cut1沒有值。截斷== 1的「值」的貢獻被埋在'(截取)'內。估計是這樣的:

> cbind(levels(diamonds$cut), c(coef(model.cut)[grep('Intercept|cut', names(coef(model.cut)))])) 
      [,1]  [,2]    
(Intercept) "Fair"  "-5189.46034442502" 
cut2  "Good"  "909.432743872746" 
cut3  "Very Good" "1129.51839934007" 
cut4  "Premium" "1156.98898349819" 
cut5  "Ideal"  "1264.12800574865" 

你可以繪製未經調整的手段,但未經調整的值並沒有真正意義,(從而強調了迴歸的需求分析):

> with(diamonds, tapply(price, cut, mean)) 
    Fair  Good Very Good Premium  Ideal 
4358.758 3928.864 3981.760 4584.258 3457.542 

所以看的carat昆泰內切的效果:

> with(diamonds, tapply(price, list(cut, cut2(carat, g=5)), mean)) 
      [0.20,0.36) [0.36,0.54) [0.54,0.91) [0.91,1.14) [1.14,5.01] 
Fair   802.4528 1193.162 2336.543 4001.972 8682.351 
Good   574.7482 1101.406 2701.412 4872.072 9788.294 
Very Good 597.9258 1151.537 2727.251 5464.223 10158.057 
Premium  717.1096 1149.550 2537.446 5214.787 10131.999 
Ideal  739.8972 1254.229 2624.180 6050.358 10317.725 

所以...什麼的效果?對於雙向分析,在「cut」的全部值範圍內可能平均爲800?

contrasts(diamonds$cut, how.many=1) <- poly(1:5) 
> model.cut2 <- lm(price ~ carat+cut, diamonds) 
> model.cut2 

Call: 
lm(formula = price ~ carat + cut, data = diamonds) 

Coefficients: 
(Intercept)  carat   cut1 
    -2555.1  7838.5  815.8 

> contrasts(diamonds$cut) 
        1 
Fair  -0.6324555 
Good  -0.3162278 
Very Good 0.0000000 
Premium 0.3162278 
Ideal  0.6324555 

的預計售價的平均差別控股carat不變公平與理想的情況是(-0.6324555 -0.6324555)* 815.8或減去1031.91(美元或歐元的價格差異....無論價格變量的單位)

我決定刪除一堆其他東西,我打算放在這裏,因爲我認爲這足以證明我的基本觀點,即需要理解底層編碼的順序妥善解釋和傳達「效應」的重要性。僅係數是沒有意義的。來自poly的線性對比創建基本上針對「完整」範圍差異的效應係數。如果使用R poly(),則需要使用對比矩陣值估計的係數進行比較。對比度範圍通常在1左右,線性對比度集中在0.

0

合理的先驗方法在這裏節省了電力,可以評估線性,四次和立方形對比度。這使得最合理的模式,避免了測試通過大量層次的允許那些高階多項式,但是這將奧康不適的威廉·如果在理論上依靠:-)

library(ggplot2) 
df = diamonds[1:1000, ] # a chunk of data 
contrasts(df$cut , how.many=3) = contr.poly(nlevels(df$cut)) 
contrasts(df$color , how.many=3) = contr.poly(nlevels(df$color)) 
contrasts(df$clarity, how.many=3) = contr.poly(nlevels(df$clarity)) 
model <- lm(price ~ carat+cut+color+clarity, data = df) 
summary(model) 


Coefficients: 
      Estimate Std. Error t value Pr(>|t|)  
(Intercept) -692.74  30.99 -22.353 < 2e-16 *** 
carat  4444.79  41.37 107.431 < 2e-16 *** 
cut.L   286.35  22.31 12.835 < 2e-16 *** 
cut.Q   -88.61  20.26 -4.374 1.35e-05 *** 
cut.C   120.91  18.51 6.532 1.03e-10 *** 
color.L  -660.17  24.93 -26.476 < 2e-16 *** 
color.Q  -119.34  23.90 -4.993 7.03e-07 *** 
color.C  37.18  20.90 1.779 0.0756 . 
clarity.L 1356.12  43.22 31.375 < 2e-16 *** 
clarity.Q -220.86  33.48 -6.596 6.87e-11 *** 
clarity.C  375.47  31.10 12.073 < 2e-16 *** 

Multiple R-squared: 0.929, Adjusted R-squared: 0.9283 
F-statistic: 1293 on 10 and 989 DF, p-value: < 2.2e-16