残差与拟合图在R中

huangapple go评论101阅读模式
英文:

residual vs fitted graph in R

问题

我想在R中绘制残差与拟合图,以展示我的数据的线性关系。我正在使用tslm()函数。以下是我的操作:

# 使用plot()函数
> plot(ts_model)
错误信息:`[.default`(X, , piv, drop = FALSE) 中:维度数量不正确

我无法使用plot()函数。我尝试了另一种方法来绘制残差与拟合图:

> #获取残差列表
> res <- resid(ts_model)
> 
> #线性关系
> plot(fitted(ts_model),res)
> abline(0,0)

然而,红线没有显示出来。我想要在图中包括红线以展示线性关系。

[![使用lm函数绘制的残差与拟合图][1]][1]

我想要类似上图的效果。这个图是使用lm函数绘制的,但我想使用tslm()函数绘制。

[![使用tslm()函数绘制的残差与拟合图][2]][2]
[![dput(ts_model)][3]][3]

  [1]: https://i.stack.imgur.com/wWPN1.png
  [2]: https://i.stack.imgur.com/oDUEY.png
  [3]: https://i.stack.imgur.com/6uci1.png
英文:

I want to plot residual vs fitted plot in R to show linearity of my data. I am using tslm() function. This is what I did.

 &gt; plot(ts_model)
 Error in `[.default`(X, , piv, drop = FALSE) : 
 incorrect number of dimensions

I am unable to use plot() function. I tried another way to plot the residual vs fitted pot.

&gt; #Get list of residuals
&gt; res &lt;- resid(ts_model)
&gt; 
&gt; #Linearity
&gt; plot(fitted(ts_model),res)
&gt; abline(0,0)

However, the red line is not showing. I want to include the red line to show the linearity.

残差与拟合图在R中

I want something like the above. This plot is using lm function but I want to plot this using tslm() fucntion.

残差与拟合图在R中
残差与拟合图在R中

答案1

得分: 0

根据plot.lm()的文档,plot.lm()函数的红线是通过设置参数add.smooth=TRUE获得的。反过来,这个参数使用panel.smooth()来生成与绘制点相适应的平滑曲线。最后,panel.smooth()的文档告诉我们执行平滑拟合的函数是lowess()

因此,只需绘制对lowess()的调用结果即可生成您想要的红线。以下代码示例(使用forecast包中help(tslm)中提供的示例数据进行演示):

# 使生成的图可复制
set.seed(1313)

# 生成示例数据
y <- ts(rnorm(120,0,3) + 1:120 + 20*sin(2*pi*(1:120)/12), frequency=12)

# 拟合tslm模型
fit <- tslm(y ~ trend + season)

# 绘制残差 vs. 拟合图
# (注意使用as.numeric(),否则点之间会被线连接)
plot(as.numeric(fitted(fit)), as.numeric(residuals(fit)), type="p")

# 添加一个平滑趋势线,调用lowess(),与plot.lm()函数一样
lines(lowess(fitted(fit), residuals(fit)), col="red")

这是生成的图表:

残差与拟合图在R中

英文:

According to the documentation of plot.lm(), the red line of the plot.lm() function is obtained when parameter add.smooth=TRUE. In turn, this parameter uses panel.smooth() to generate a smoothed curve that fits the plotted points. Finally, the documentation of panel.smooth() tells us that the function responsible for doing the smooth fit is lowess().

Therefore, a line that plots the result of the call to lowess() should suffice to generate the red line that you are after. The following code illustrates (showcased with the sample data given in help(tslm) of the forecast package):

# Make generated plot reproducible
set.seed(1313)

# Generate the sample data
y &lt;- ts(rnorm(120,0,3) + 1:120 + 20*sin(2*pi*(1:120)/12), frequency=12)

# Fit the tslm model
fit &lt;- tslm(y ~ trend + season)

# Plot the residuals vs. fitted graph
# (note the use of as.numeric(), o.w. the points are connected by lines)
plot(as.numeric(fitted(fit)), as.numeric(residuals(fit)), type=&quot;p&quot;)

# Add a smooth trend line calling lowess(), as used by the plot.lm() function
lines(lowess(fitted(fit), residuals(fit)), col=&quot;red&quot;)

This is the resulting plot:

残差与拟合图在R中

huangapple
  • 本文由 发表于 2023年7月3日 21:31:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定