VIF 在 R 中返回别名系数
VIFs returning aliased coefficients in R
我想知道是否有人可以帮助我解决以下问题。当我在各种解释变量之间进行 VIF 分析时,会出现以下错误消息。
test <-vif(lm(Spring_Autumn ~ Oct + Nov + Dec + Jan + Feb +
Mar + Apr + May + Jun + Jul + Aug + Sep + X1min + X3min + X7min + X30min + X90min + X1max + X3max + X7max + X30max + X90max + BF + Dmin + Dmax+ LP + LPD + HP + HPD + RR + FR + Rev, data = IHA_stats))
Error in vif.default(lm(Spring_Autumn ~ Oct + Nov + Dec + Jan + Feb + :
there are aliased coefficients in the model
在线阅读后,似乎我有两个完全共线的变量,但我看不到 2 个变量通过 cor 函数完全相关,现在不知道如何解释别名函数 table .有没有人有什么建议?先感谢您。
James(原始数据集的 link 粘贴在下面,但如果访问此数据集有任何问题,可以通过电子邮件发送)。
https://www.dropbox.com/s/nqmagu9m3mjhy9n/IHA_statistics.csv?dl=0
使用 R 中的 'alias' 函数查看哪些变量是线性相关的。移除因变量,vif 函数应该可以正常工作。
formula <- as.formula(Spring_Autumn ~ Oct + Nov + Dec + Jan + Feb + Mar + Apr + May + Jun + Jul + Aug + Sep + X1min + X3min + X7min + X30min + X90min + X1max + X3max + X7max + X30max + X90max + BF + Dmin + Dmax+ LP + LPD + HP + HPD + RR + FR + Rev, data = IHA_stats)
fit <-lm(formula)
#the linearly dependent variables
ld.vars <- attributes(alias(fit)$Complete)$dimnames[[1]]
#remove the linearly dependent variables variables
formula.new <- as.formula(
paste(
paste(deparse(formula), collapse=""),
paste(ld.vars, collapse="-"),
sep="-"
)
)
#run model again
fit.new <-lm(formula.new)
vif(fit.new)
注意:如果您自动生成与其他变量相同的虚拟变量,这将不起作用。变量名搞砸了。您可以创建自己的 hack 来绕过它。
我想知道是否有人可以帮助我解决以下问题。当我在各种解释变量之间进行 VIF 分析时,会出现以下错误消息。
test <-vif(lm(Spring_Autumn ~ Oct + Nov + Dec + Jan + Feb +
Mar + Apr + May + Jun + Jul + Aug + Sep + X1min + X3min + X7min + X30min + X90min + X1max + X3max + X7max + X30max + X90max + BF + Dmin + Dmax+ LP + LPD + HP + HPD + RR + FR + Rev, data = IHA_stats))
Error in vif.default(lm(Spring_Autumn ~ Oct + Nov + Dec + Jan + Feb + :
there are aliased coefficients in the model
在线阅读后,似乎我有两个完全共线的变量,但我看不到 2 个变量通过 cor 函数完全相关,现在不知道如何解释别名函数 table .有没有人有什么建议?先感谢您。
James(原始数据集的 link 粘贴在下面,但如果访问此数据集有任何问题,可以通过电子邮件发送)。
https://www.dropbox.com/s/nqmagu9m3mjhy9n/IHA_statistics.csv?dl=0
使用 R 中的 'alias' 函数查看哪些变量是线性相关的。移除因变量,vif 函数应该可以正常工作。
formula <- as.formula(Spring_Autumn ~ Oct + Nov + Dec + Jan + Feb + Mar + Apr + May + Jun + Jul + Aug + Sep + X1min + X3min + X7min + X30min + X90min + X1max + X3max + X7max + X30max + X90max + BF + Dmin + Dmax+ LP + LPD + HP + HPD + RR + FR + Rev, data = IHA_stats)
fit <-lm(formula)
#the linearly dependent variables
ld.vars <- attributes(alias(fit)$Complete)$dimnames[[1]]
#remove the linearly dependent variables variables
formula.new <- as.formula(
paste(
paste(deparse(formula), collapse=""),
paste(ld.vars, collapse="-"),
sep="-"
)
)
#run model again
fit.new <-lm(formula.new)
vif(fit.new)
注意:如果您自动生成与其他变量相同的虚拟变量,这将不起作用。变量名搞砸了。您可以创建自己的 hack 来绕过它。