rms包中具有交互作用的线性回归失败 [英] Linear regression with interaction fails in the rms-package

查看:286
本文介绍了rms包中具有交互作用的线性回归失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在公式中进行交互.我想知道是否有可能对两个虚拟变量之一进行交互作用的回归.这似乎在使用lm()函数的常规线性回归中有效,但在rms软件包中使用ols()函数时,相同的公式将失败.有人知道为什么吗?

I'm playing around with interaction in the formula. I wondered if it's possible to do a regression with interaction for one of the two dummy variables. This seems to work in regular linear regression using the lm() function but with the ols() function in the rms package the same formula fails. Anyone know why?

这是我的例子

data(mtcars)

mtcars$gear <- factor(mtcars$gear)
regular_lm <- lm(mpg ~ wt + cyl + gear + cyl:gear, data=mtcars)
summary(regular_lm)

regular_lm <- lm(mpg ~ wt + cyl + gear + cyl:I(gear == "4"), data=mtcars)
summary(regular_lm)

现在是rms示例

library(rms)

dd <- datadist(mtcars)
options(datadist = "dd")

regular_ols <- ols(mpg ~ wt + cyl + gear + cyl:gear, data=mtcars)
regular_ols

# Fails with:
#     Error in if (!length(fname) || !any(fname == zname)) { : 
#         missing value where TRUE/FALSE needed
regular_ols <- ols(mpg ~ wt + cyl + gear + cyl:I(gear == "4"), data=mtcars)

该实验可能不是最明智的统计方法,因为估计值似乎发生了很大变化,但是我有点好奇ols()为何会失败,因为它应该执行与lm所用的拟合程序相同的事情"

This experiment might not be the wisest statistic to do as it seems that the estimates change significantly but I'm a little curious to why ols() fails since it should do the "same fitting routines used by lm"

推荐答案

我不清楚,但它与公式求值的方式有关,而不是与模型一旦完成就适合的方式有关已翻译.使用traceback()表示问题出现在Design(eval.parent(m))内;使用options(error=recover)将您带到可以看到的地方

I don't know exactly, but it has to do with the way the formula is evaluated rather than with the way the fit is done once the model has been translated. Using traceback() shows that the problem occurs within Design(eval.parent(m)); using options(error=recover) gets you to the point where you can see that

Browse[1]> fname
[1] "wt"   "cyl"  "gear"
Browse[1]> zname
[1] NA

换句话说,zname是一些尚未正确设置的内部变量,因为Design函数不能完全处理定义圆柱体与(gear == 4)假人之间的相互作用的动态操作.

in other words, zname is some internal variable that hasn't been set right because the Design function can't quite handle defining the interaction between cylinders and the (gear==4) dummy on the fly.

这可以:

mtcars$cylgr <- with(mtcars,interaction(cyl,gear == "4"))
regular_ols <- ols(mpg ~ wt + cyl + gear + cylgr, data=mtcars)

这篇关于rms包中具有交互作用的线性回归失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆