我已经找到了Error in contrasts when defining a linear model in R,并遵循了那里的建议,但我的因子变量中没有一个只有一个值,我仍然遇到同样的问题。
这是我使用的数据集:https://www.dropbox.com/s/em7xphbeaxykgla/train.csv?dl=0。
这是我试图运行的代码:
simplelm <- lm(log_SalePrice ~ ., data = train)
#Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
# contrasts can be applied only to factors with 2 or more levels问题出在哪里?
发布于 2018-05-19 08:34:19
这个错误很好地描述了这个问题。问题中的坏数据在第9列(Utilities)。
这一栏的变化太小了。
table(train$Utilities)AllPub NoSeWa 1459 %1
log_SalePrice <- train$log_SalePrice
train[,9] <- NULL
simplelm <- lm(log_SalePrice ~ ., data = train)发布于 2018-07-29 03:10:17
感谢您提供您的数据集(我希望该链接将永远有效,以便每个人都可以访问)。我将其读取到数据帧train中。
使用How to debug "contrasts can be applied only to factors with 2 or more levels" error?提供的debug_contr_error、debug_contr_error2和NA_preproc帮助器函数,我们可以很容易地分析问题。
info <- debug_contr_error2(log_SalePrice ~ ., train)
## the data frame that is actually used by `lm`
dat <- info$mf
## number of cases in your dataset
nrow(train)
#[1] 1460
## number of complete cases used by `lm`
nrow(dat)
#[1] 1112
## number of levels for all factor variables in `dat`
info$nlevels
# MSZoning Street Alley LotShape LandContour
# 4 2 3 4 4
# Utilities LotConfig LandSlope Neighborhood Condition1
# 1 5 3 25 9
# Condition2 BldgType HouseStyle RoofStyle RoofMatl
# 6 5 8 5 7
# Exterior1st Exterior2nd MasVnrType ExterQual ExterCond
# 14 16 4 4 4
# Foundation BsmtQual BsmtCond BsmtExposure BsmtFinType1
# 6 5 5 5 7
# BsmtFinType2 Heating HeatingQC CentralAir Electrical
# 7 5 5 2 5
# KitchenQual Functional FireplaceQu GarageType GarageFinish
# 4 6 6 6 3
# GarageQual GarageCond PavedDrive PoolQC Fence
# 5 5 3 4 5
# MiscFeature SaleType SaleCondition MiscVal_bool MoYrSold
# 4 9 6 2 55 正如您所看到的,Utilities在这里是一个有问题的变量,因为它只有一个级别。
由于您在train中有许多字符/因子变量,我想知道您是否为它们提供了NA。如果我们添加NA作为有效级别,我们可能会得到更完整的案例。
new_train <- NA_preproc(train)
new_info <- debug_contr_error2(log_SalePrice ~ ., new_train)
new_dat <- new_info$mf
nrow(new_dat)
#[1] 1121
new_info$nlevels
# MSZoning Street Alley LotShape LandContour
# 5 2 3 4 4
# Utilities LotConfig LandSlope Neighborhood Condition1
# 1 5 3 25 9
# Condition2 BldgType HouseStyle RoofStyle RoofMatl
# 6 5 8 5 7
# Exterior1st Exterior2nd MasVnrType ExterQual ExterCond
# 14 16 4 4 4
# Foundation BsmtQual BsmtCond BsmtExposure BsmtFinType1
# 6 5 5 5 7
# BsmtFinType2 Heating HeatingQC CentralAir Electrical
# 7 5 5 2 6
# KitchenQual Functional FireplaceQu GarageType GarageFinish
# 4 6 6 6 3
# GarageQual GarageCond PavedDrive PoolQC Fence
# 5 5 3 4 5
# MiscFeature SaleType SaleCondition MiscVal_bool MoYrSold
# 4 9 6 2 55我们确实得到了更完整的案例,但Utilities仍然有一个级别。这意味着大多数不完整的情况实际上是由您的数值变量中的NA引起的,我们无能为力(除非您有一种统计上有效的方法来估算这些缺失值)。
由于您只有一个单级因子变量,因此与How to do a GLM when "contrasts can be applied only to factors with 2 or more levels"?中给出的方法相同。
new_dat$Utilities <- 1
simplelm <- lm(log_SalePrice ~ 0 + ., data = new_dat)现在,模型已成功运行。然而,它是rank-deficient。您可能想做些什么来解决这个问题,但让它保持原样是可以的。
b <- coef(simplelm)
length(b)
#[1] 301
sum(is.na(b))
#[1] 9
simplelm$rank
#[1] 292https://stackoverflow.com/questions/50420661
复制相似问题