2017-04-04 17 views
1

car包,我試圖通過lm功能預測稱爲prestige響應變量數據集中基礎上incomeeducation也叫Prestige,並且因素type。但在我符合數據之前,我想要縮放educationincome。如果複製和R中stuido運行下面的代碼時,控制檯會說Error: variables ‘income’, ‘I(income^2)’, ‘education’, ‘I(education^2)’ were specified with different types from the fit錯誤:變量與不同類型的規定從擬合

library(car) 
summary(Prestige) 
Prestige$education <- scale(Prestige$education) 

Prestige$income <- scale(Prestige$income) 

fit <- lm(prestige ~ income + I(income^2) + education + I(education^2) 
      + income:education + type + type:income + type:I(income^2) 
      + type:education + type:I(education^2)+ type:income:education, Prestige) 
summary(fit) 
pred <- expand.grid(income = c(1000, 20000), education = c(10,20),type = levels(Prestige $ type)) 
pred $ prestige.pred <- predict(fit, newdata = pred) 
pred 

無調整的預測,它可以成功地工作。所以這個錯誤肯定是由於預測之前的縮放比例造成的,我想知道如何解決這個問題?

回答

3

請注意scale()實際上會更改您的列的類別。見

class(car::Prestige$education) 
# [1] "numeric" 
class(scale(car::Prestige$education)) 
# [1] "matrix" 

你會安全地將它們簡化爲數字向量。您可以使用c()維剝離性能這個

Prestige$education <- c(scale(Prestige$education)) 
Prestige$income <- c(scale(Prestige$income)) 

然後,我能夠與

fit <- lm(prestige ~ income + I(income^2) + education + I(education^2) 
      + income:education + type + type:income + type:I(income^2) 
      + type:education + type:I(education^2)+ type:income:education, 
      Prestige, na.action="na.omit") 

運行模型和預測返回

income education type prestige.pred 
1 1000  10 bc -1352364.5 
2 20000  10 bc -533597423.4 
3 1000  20 bc -1382361.7 
4 20000  20 bc -534229639.3 
5 1000  10 prof  398464.2 
6 20000  10 prof 155567014.1 
7 1000  20 prof  409271.3 
8 20000  20 prof 155765754.7 
9 1000  10 wc -7661464.3 
10 20000  10 wc -3074382169.9 
11 1000  20 wc -7634693.8 
12 20000  20 wc -3073902696.6 

還要注意你凸輪用

fit<-lm(prestige ~ (income + I(income^2) + education + I(education^2))*type + 
      income:education + type:income:education, Prestige, na.action="na.omit") 
簡化你的公式

這使用*來創建許多交互術語。

2

scale()lm()中添加了似乎產生問題的屬性。使用

Prestige$education <- as.numeric(scale(Prestige$education))  
Prestige$education <- as.numeric(scale(Prestige$income)) 

使一切正常。