如何解决regsubsets从因子水平生成虚拟变量-我们可以使其不这样做吗?
来自regsubsets
包的函数leaps
将类别(因子)变量的所有级别视为独立的虚拟变量。我想改变这种行为。
使用iris
数据集的示例,其中Species
是因子变量:
library(leaps)
data(iris)
models <- regsubsets( Sepal.Length~.,data = iris,nvmax = 4)
summary(models)
Subset selection object
Call: regsubsets.formula(Sepal.Length ~ .,nvmax = 4)
5 Variables (and intercept)
Forced in Forced out
Sepal.Width FALSE FALSE
Petal.Length FALSE FALSE
Petal.Width FALSE FALSE
Speciesversicolor FALSE FALSE
Speciesvirginica FALSE FALSE
1 subsets of each size up to 4
Selection Algorithm: exhaustive
Sepal.Width Petal.Length Petal.Width Speciesversicolor Speciesvirginica
1 ( 1 ) " " "*" " " " " " "
2 ( 1 ) "*" "*" " " " " " "
3 ( 1 ) "*" "*" "*" " " " "
4 ( 1 ) "*" "*" " " "*" "*"
请注意,regsubsets
创建了伪变量Speciesversicolor
和Speciesvirginica
,它们现在占据了第四行变量的四个“空格”中的两个。我希望Species
只占一个空格。
是否可以更改regsubsets
函数的这种行为?
之前也曾提出过类似的问题,但大多数评论(和我)都同意该问题仍未得到解答: https://stats.stackexchange.com/questions/152158/r-model-selection-with-categorical-variables-using-leaps-and-glmnet
这是另一个类似的未解决问题:R: can I get regsubsets() to in-/exclude variables by groups?
解决方法
leaps仅适用于定量变量。 Venables和Ripley在 Modern Applied Statistics with S 中特别提到了这一点-在索引中查找 leaps 。
对于基于drop
和add
的逐步选择,这些选择将包括或排除所有级别的任何因素,而使用step
或在MASS stepAIC
中。
fm <- lm(Sepal.Length ~.,iris)
step(fm)
,
正如G.Grothendieck所说,“游隙仅适用于定量变量”。
但是,还有其他软件包可以使用最佳子集回归来执行特征选择,而不能处理具有多个级别的分类变量。
使用olsrr
包和示例数据,只需一行代码即可完成
library(olsrr)
data("iris")
model <- lm(Sepal.Length ~ .,data = iris)
ols_step_best_subset(model)
#> Best Subsets Regression
#> -----------------------------------------------------------
#> Model Index Predictors
#> -----------------------------------------------------------
#> 1 Petal.Length
#> 2 Sepal.Width Petal.Length
#> 3 Sepal.Width Petal.Length Species
#> 4 Sepal.Width Petal.Length Petal.Width Species
#> -----------------------------------------------------------
#>
#> Subsets Regression Summary
#> -----------------------------------------------------------------------------------------------------------------------------------
#> Adj. Pred
#> Model R-Square R-Square R-Square C(p) AIC SBIC SBC MSEP FPE HSP APC
#> -----------------------------------------------------------------------------------------------------------------------------------
#> 1 0.7600 0.7583 0.7532 114.5104 160.0404 -267.6979 169.0723 24.8565 0.1679 0.0011 0.2465
#> 2 0.8402 0.8380 0.834 29.4478 101.0255 -325.5037 113.0680 16.6628 0.1133 8e-04 0.1663
#> 3 0.8633 0.8595 0.8536 6.3448 81.5749 -346.0176 99.6387 14.3495 0.0989 7e-04 0.1442
#> 4 0.8673 0.8627 0.8554 4.0000 79.1160 -348.1523 100.1905 14.0259 0.0973 7e-04 0.1418
#> -----------------------------------------------------------------------------------------------------------------------------------
#> AIC: Akaike Information Criteria
#> SBIC: Sawa's Bayesian Information Criteria
#> SBC: Schwarz Bayesian Criteria
#> MSEP: Estimated error of prediction,assuming multivariate normality
#> FPE: Final Prediction Error
#> HSP: Hocking's Sp
#> APC: Amemiya Prediction Criteria
我们还可以使用所有可能的子集特征选择:
ols_step_all_possible(model)
#> Index N Predictors R-Square
#> 2 1 1 Petal.Length 0.75995465
#> 3 2 1 Petal.Width 0.66902769
#> 4 3 1 Species 0.61870573
#> 1 4 1 Sepal.Width 0.01382265
#> 5 5 2 Sepal.Width Petal.Length 0.84017784
#> 9 6 2 Petal.Length Species 0.83672378
#> 8 7 2 Petal.Length Petal.Width 0.76626130
#> 7 8 2 Sepal.Width Species 0.72590661
#> 6 9 2 Sepal.Width Petal.Width 0.70723708
#> 10 10 2 Petal.Width Species 0.66936637
#> 12 11 3 Sepal.Width Petal.Length Species 0.86330878
#> 11 12 3 Sepal.Width Petal.Length Petal.Width 0.85861172
#> 14 13 3 Petal.Length Petal.Width Species 0.83672544
#> 13 14 3 Sepal.Width Petal.Width Species 0.73238452
#> 15 15 4 Sepal.Width Petal.Length Petal.Width Species 0.86731226
#> Adj. R-Square Mallow's Cp
#> 2 0.758332718 114.510364
#> 3 0.666791387 213.189280
#> 4 0.613518054 267.801422
#> 1 0.007159294 924.253661
#> 5 0.838003384 29.447765
#> 9 0.833368794 33.196290
#> 8 0.763081179 109.666040
#> 7 0.720274553 153.461158
#> 6 0.703253908 173.722356
#> 10 0.662572526 214.821724
#> 12 0.859537986 6.344799
#> 11 0.855706481 11.442304
#> 14 0.832221316 35.194491
#> 13 0.725002020 148.430978
#> 15 0.862705049 4.000000
由reprex package(v0.3.0)于2020-10-07创建
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。