plot.lm错误:$运算符对原子向量无效 [英] plot.lm Error: $ operator is invalid for atomic vectors
问题描述
我有以下带有转换的回归模型:
I have the following regression model with transformations:
fit <- lm( I(NewValue ^ (1 / 3)) ~ I(CurrentValue ^ (1 / 3)) + Age + Type - 1,
data = dataReg)
plot(fit)
但是plot
给我以下错误:
Error: $ operator is invalid for atomic vectors
关于我在做什么错的任何想法吗?
Any ideas about what I am doing wrong?
注意:summary
,predict
和residuals
都可以正常工作.
Note: summary
, predict
, and residuals
all work correctly.
推荐答案
这实际上是一个非常有趣的观察.实际上,在plot.lm
支持的所有6个图中,在这种情况下只有Q-Q图失败.考虑以下可重现的示例:
This is actually quite a interesting observation. In fact, among all 6 plots supported by plot.lm
, only the Q-Q plot fails in this case. Consider the following reproducible example:
x <- runif(20)
y <- runif(20)
fit <- lm(I(y ^ (1/3)) ~ I(x ^ (1/3)))
## only `which = 2L` (QQ plot) fails; `which = 1, 3, 4, 5, 6` all work
stats:::plot.lm(fit, which = 2L)
在plot.lm
内,Q-Q图简单生成如下:
Inside plot.lm
, the Q-Q plot is simply produced as follow:
rs <- rstandard(fit) ## standardised residuals
qqnorm(rs) ## fine
## inside `qqline(rs)`
yy <- quantile(rs, c(0.25, 0.75))
xx <- qnorm(c(0.25, 0.75))
slope <- diff(yy)/diff(xx)
int <- yy[1L] - slope * xx[1L]
abline(int, slope) ## this fails!!!
错误:$运算符对于原子向量无效
Error: $ operator is invalid for atomic vectors
所以这纯粹是abline
函数的问题!注意:
So this is purely a problem of abline
function! Note:
is.object(int)
# [1] TRUE
is.object(slope)
# [1] TRUE
即int
和slope
都具有类属性(读取?is.object
;这是检查对象是否具有类属性的一种非常有效的方法).什么课?
i.e., both int
and slope
has class attribute (read ?is.object
; it is a very efficient way to check whether an object has class attribute). What class?
class(int)
# [1] AsIs
class(slope)
# [1] AsIs
这是使用I()
的结果.确切地说,它们从rs
继承了此类,并且进一步从response变量继承了此类.也就是说,如果我们在响应中使用I()
,即模型公式的RHS,则会得到此行为.
This is the result of using I()
. Precisely, they inherits such class from rs
and further from the response variable. That is, if we use I()
on response, the RHS of the model formula, we get this behaviour.
您可以在此处进行一些实验:
You can do a few experiment here:
abline(as.numeric(int), as.numeric(slope)) ## OK
abline(as.numeric(int), slope) ## OK
abline(int, as.numeric(slope)) ## fails!!
abline(int, slope) ## fails!!
因此,abline(a, b)
对于第一个参数a
是否具有类属性非常敏感.
So abline(a, b)
is very sensitive to whether the first argument a
has class attribute or not.
为什么?因为abline
可以接受带有"lm"类的线性模型对象.内abline
:
Why? Because abline
can accept a linear model object with "lm" class. Inside abline
:
if (is.object(a) || is.list(a)) {
p <- length(coefa <- as.vector(coef(a)))
如果a
具有类,则abline
会将其假定为模型对象(无论它是否真的是!!!),然后尝试使用coef
来获取系数.此处进行的检查相当不可靠;我们可以很容易地使abline
失败:
If a
has a class, abline
is assuming it as a model object (regardless whether it is really is!!!), then try to use coef
to obtain coefficients. The check being done here is fairly not robust; we can make abline
fail rather easily:
plot(0:1, 0:1)
a <- 0 ## plain numeric
abline(a, 1) ## OK
class(a) <- "whatever" ## add a class
abline(a, 1) ## oops, fails!!!
错误:$运算符对于原子向量无效
Error: $ operator is invalid for atomic vectors
因此,得出的结论是:避免在模型公式的响应变量上使用I()
.可以在协变量上使用I()
,而不必在响应上使用I()
. lm
和大多数通用函数都不会遇到麻烦,但是plot.lm
会解决.
So here is the conclusion: avoid using I()
on your response variable in the model formula. It is OK to have I()
on covariates, but not on response. lm
and most generic functions won't have trouble dealing with this, but plot.lm
will.
这篇关于plot.lm错误:$运算符对原子向量无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!