2017-04-12 17 views
0

データセットがありますhttps://www.kaggle.com/c/house-prices-advanced-regression-techniques/data 2つ以上のレベルと1つのターゲット値である2つの要因、SalePrice 。2つ以上のレベルが存在する場合、 "コントラストは2つ以上のレベルの因子にのみ適用できます"エラー

Street  Alley  SalePrice  
Grvl: 6 Grvl: 50 Min. : 34900 
Pave:1454 Pave: 41 1st Qu.:129975 
      NA's:1369 Median :163000 
         Mean :180921 
         3rd Qu.:214000 
         Max. :755000 

2つの要因について線形回帰を別々に実行すると、正常に動作します。

> summary(lm(SalePrice ~ Street, data=train)) 

Call: 
lm(formula = SalePrice ~ Street, data = train) 

Residuals: 
    Min  1Q Median  3Q  Max 
-146231 -51131 -18131 32869 573869 

Coefficients: 
      Estimate Std. Error t value Pr(>|t|)  
(Intercept) 130190  32416 4.016 6.21e-05 *** 
StreetPave  50940  32483 1.568 0.117  
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 79400 on 1458 degrees of freedom 
Multiple R-squared: 0.001684, Adjusted R-squared: 0.0009992 
F-statistic: 2.459 on 1 and 1458 DF, p-value: 0.117 

> summary(lm(SalePrice ~ Alley, data=train)) 

Call: 
lm(formula = SalePrice ~ Alley, data = train) 

Residuals: 
    Min  1Q Median  3Q  Max 
-128001 -17001 1781 16999 133781 

Coefficients: 
      Estimate Std. Error t value Pr(>|t|)  
(Intercept) 122219  5153 23.718 < 2e-16 *** 
AlleyPave  45782  7677 5.963 4.9e-08 *** 
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 36440 on 89 degrees of freedom 
    (1369 observations deleted due to missingness) 
Multiple R-squared: 0.2855, Adjusted R-squared: 0.2775 
F-statistic: 35.56 on 1 and 89 DF, p-value: 4.9e-08 

ただし、一緒に実行するとエラーになりますが意味をなさないものです。

> summary(lm(SalePrice ~ Street+Alley, data=train)) 
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
    contrasts can be applied only to factors with 2 or more levels 

誰かがこれを助けることができますか?私が問題になっている。このラインからヒントを得た

+1

「Alley」値がないObsがモデルから削除されます。これには 'Grvl'の通りがすべて含まれているので、モデルには2つのレベルがありません。 – Marius

答えて

0

:LMで (原因missingnessに削除された1369の観測)

を、欠損値を単純に削除されます。 StreetとAlleyでlmを実行している間に、NAはAlleyによって削除され、Street factorの単一値が得られました。

> train[!is.na(Alley), Street] 
[1] Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave 
[16] Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave 
[31] Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave 
[46] Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave 
[61] Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave 
[76] Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave Pave 
[91] Pave 
Levels: Grvl Pave 
関連する問題