lm图'x'是一个列表,但没有分量'x'和'y'

问题描述 投票:0回答:2

我看到已经提出了这个主题,但是通过阅读讨论并不能解决我的问题。即使我使用网络上的简单示例,例如,我也会一遍又一遍地收到此错误消息。来自https://medium.com/data-distilled/residual-plots-part-4-residuals-vs-leverage-plot-14aeed009ef7

model <- glm(dist ~ speed, data = cars, family = gaussian)

plot(model)

Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' is a list, but does not have components 'x' and 'y' 

命令“ plot”实际上应该“知道”如何处理对象“ model”,不是吗?

与lm相同:

x = runif(200,0,2)

y = 2*x + 1 + rnorm(200,0,1)

res = lm(y ~ x) 

plot(res)

Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' is a list, but does not have components 'x' and 'y'

class(res)

[1] "lm"

methods(class = "lm")    # plot is among them!

qqPlot(res)

Error in model.frame.default(formula = y ~ x, na.action = "na.exclude",  : 
  invalid type (list) for variable 'x' 

有什么建议吗?

r plot glm lm
2个回答
0
投票

您为什么要绘制模型?模型确实是一个复杂的列表:

model

Call:  glm(formula = dist ~ speed, family = gaussian, data = cars)

Coefficients:
(Intercept)        speed  
    -17.579        3.932 
Degrees of Freedom: 49 Total (i.e. Null);  48 Residual
Null Deviance:      32540 
Residual Deviance: 11350    AIC: 419.2 

您可以绘制的是y ~ x,例如:

plot(dist ~ speed, data = cars)

enter image description here


0
投票

这很奇怪。两组代码都对我有用。由于我无法复制您的环境,因此我只能建议您可以尝试的几件事:

  • 如果尚未重新启动R会话,请重新启动
  • 如果与任何R Studio配置设置相关,请使用基本的R IDE
  • 指定名称空间,以防名称空间冲突引起错误,例如stats::lm代替lm
  • [R通常在将对象命名为通用函数时会给您带来bug(例如,命名数据帧df是bug的常见来源)->尝试以其他方式命名您的对象]]
© www.soinside.com 2019 - 2024. All rights reserved.