Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R plot.lm错误:$运算符对原子向量无效_R_Plot_Regression_Linear Regression_Lm - Fatal编程技术网

R plot.lm错误:$运算符对原子向量无效

R plot.lm错误:$运算符对原子向量无效,r,plot,regression,linear-regression,lm,R,Plot,Regression,Linear Regression,Lm,我有以下带有转换的回归模型: fit <- lm( I(NewValue ^ (1 / 3)) ~ I(CurrentValue ^ (1 / 3)) + Age + Type - 1, data = dataReg) plot(fit) 你知道我做错了什么吗 注意:摘要,预测,以及残差都能正常工作。这实际上是一个非常有趣的观察

我有以下带有转换的回归模型:

fit <- lm( I(NewValue ^ (1 / 3)) ~ I(CurrentValue ^ (1 / 3)) + Age + Type - 1,
           data = dataReg)
plot(fit)                                                                      
你知道我做错了什么吗


注意
摘要
预测
,以及
残差
都能正常工作。

这实际上是一个非常有趣的观察结果。事实上,在
plot.lm
支持的所有6个绘图中,只有Q-Q绘图在这种情况下失败。考虑下面的可重复的例子:

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)
错误:$运算符对于原子向量无效

所以这纯粹是
abline
函数的问题
注意:

is.object(int)
# [1] TRUE

is.object(slope)
# [1] TRUE
i、 例如,
int
slope
都有class属性(读取
?is.object
;这是检查对象是否有class属性的非常有效的方法)。什么课

class(int)
# [1] AsIs

class(slope)
# [1] AsIs
这是使用
I()
的结果。确切地说,它们从
rs
继承此类,并进一步从响应变量继承此类。也就是说,如果我们对响应使用
I()
,模型公式的RHS,我们就会得到这种行为。

你可以在这里做一些实验:

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
是否具有class属性非常敏感。

为什么??因为
abline
可以接受具有“lm”类的线性模型对象。内部
abline

if (is.object(a) || is.list(a)) {
    p <- length(coefa <- as.vector(coef(a)))
错误:$运算符对于原子向量无效


因此结论如下:避免在模型公式中对响应变量使用
I()
在协变量上使用
I()
是可以的,但在响应上不可以
lm
和大多数通用函数在处理这个问题上不会有问题,但是
plot.lm
会。

你能发布
summary fit
吗?@Christoph我正要发布摘要,但李哲远的答案充分补充了我的帖子。事实上,剩余拟合图是正确生成的,而绘制Q-Q图时遇到问题。当我避开时,一切都很顺利。
abline(as.numeric(int), as.numeric(slope))  ## OK
abline(as.numeric(int), slope)  ## OK
abline(int, as.numeric(slope))  ## fails!!
abline(int, slope)  ## fails!!
if (is.object(a) || is.list(a)) {
    p <- length(coefa <- as.vector(coef(a)))
plot(0:1, 0:1)
a <- 0  ## plain numeric
abline(a, 1)  ## OK
class(a) <- "whatever"  ## add a class
abline(a, 1)  ## oops, fails!!!