【问题标题】:How to create a plot consisting of multiple residuals?如何创建由多个残差组成的图?
【发布时间】:2014-03-15 14:50:56
【问题描述】:

如何根据以下(这里的y_hat和e是什么)制作残差图?

这也是残差图的一种形式吗?

beeflm=lm(PBE ~ CBE + PPO + CPO + PFO +DINC + CFO+RDINC+RFP+YEAR, data = beef)
summary(beeflm)
qqnorm(residuals(beeflm))
#plot(beeflm) #in manuals I have seen they use this but it gives me multiple plot

或者这个是正确的吗?

plot(beeflm$residuals,beeflm$fitted.values)

我通过 cmets 知道 plot(beeflm,which=1) 是正确的,但根据所述问题我应该使用 matplot 但我收到以下错误:

matplot(beeflm,which=1,
+         main = "Beef: residual plot",
+         ylab = expression(e[i]), # only 1st is taken
+         xlab = expression(hat(y[i])))

Error in xy.coords(x, y, xlabel, ylabel, log = log) : 
  (list) object cannot be coerced to type 'double'

当我使用plot 时,我收到以下错误:

plot(beeflm,which=1,main="Beef: residual plot",ylab = expression(e[i]),xlab = expression(hat(y[i])))
Error in plot.default(yh, r, xlab = l.fit, ylab = "Residuals", main = main,  : 
  formal argument "xlab" matched by multiple actual arguments

你也知道下面是什么意思吗?有什么例子可以说明这一点(或外部链接)?

牛肉数据如下:

这是牛肉的data.frame:

   YEAR  PBE  CBE  PPO  CPO  PFO DINC  CFO RDINC RFP
1  1925 59.7 58.6 60.5 65.8 65.8 51.4 90.9  68.5 877
2  1926 59.7 59.4 63.3 63.3 68.0 52.6 92.1  69.6 899
3  1927 63.0 53.7 59.9 66.8 65.5 52.1 90.9  70.2 883
4  1928 71.0 48.1 56.3 69.9 64.8 52.7 90.9  71.9 884
5  1929 71.0 49.0 55.0 68.7 65.6 55.1 91.1  75.2 895
6  1930 74.2 48.2 59.6 66.1 62.4 48.8 90.7  68.3 874
7  1931 72.1 47.9 57.0 67.4 51.4 41.5 90.0  64.0 791
8  1932 79.0 46.0 49.5 69.7 42.8 31.4 87.8  53.9 733
9  1933 73.1 50.8 47.3 68.7 41.6 29.4 88.0  53.2 752
10 1934 70.2 55.2 56.6 62.2 46.4 33.2 89.1  58.0 811
11 1935 82.2 52.2 73.9 47.7 49.7 37.0 87.3  63.2 847
12 1936 68.4 57.3 64.4 54.4 50.1 41.8 90.5  70.5 845
13 1937 73.0 54.4 62.2 55.0 52.1 44.5 90.4  72.5 849
14 1938 70.2 53.6 59.9 57.4 48.4 40.8 90.6  67.8 803
15 1939 67.8 53.9 51.0 63.9 47.1 43.5 93.8  73.2 793
16 1940 63.4 54.2 41.5 72.4 47.8 46.5 95.5  77.6 798
17 1941 56.0 60.0 43.9 67.4 52.2 56.3 97.5  89.5 830

【问题讨论】:

  • 看看plot(beeflm)。并且永远不要将数据添加为屏幕截图,因为那是无用的。按照the faq 中的建议提供数据。
  • 使用dput(beaf),然后提供R 输出以用作数据。见here
  • 这些是指导您完成建模过程和诊断的一组很好的规则。

标签: r plot statistics dataframe regression


【解决方案1】:

使用plot(beeflm, which=1) 获取残差和拟合值之间的图。

require(graphics)

## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
plot(lm.D9, which=1)

已编辑

您可以使用matplot,如下所示:

matplot(
  x = lm.D9$fitted.values
, y = lm.D9$resid
)

【讨论】:

  • @Mona:以后请提供最低工作示例 (MWE)。
  • 我用收到的错误更新了问题,你能看一下吗?谢谢!
【解决方案2】:

使用mtcars 数据的示例

fit <- lm(mpg ~ ., data=mtcars)
plot(x=fitted(fit), y=residuals(fit))

par(mfrow=c(3,4)) # or 'layout(matrix(1:12, nrow=3, byrow=TRUE))'
for (coeff in colnames(mtcars)[-1])
  plot(x=mtcars[, coeff], residuals(fit),  xlab=coeff, ylab=expression(e[i]))

【讨论】:

  • plot 之前缺少fit &lt;- lm(mpg ~ ., data=mtcars)。无法编辑,因为 SO 似乎已损坏 atm。
猜你喜欢
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 2017-11-06
相关资源
最近更新 更多