错误:类型流明的对象不自动绘制支持(Error: Objects of type lm not su

2019-09-29 19:28发布

我试图创建一个接受两个参数的简单函数:

的线性模型和对应于一种颜色,在该顺序的字符串。

它应为此创造线性模型的残差VS合身而QQ图和彩色之分,根据字符串参数是这样的:

demo_lm < - LM(ExpenditurePerStud〜国家,数据= clean_colleges)

answer7(demo_lm, “#34925E”)

这是我曾尝试:

library(ggplot2)

demo_lm <- lm(ExpenditurePerStud ~ State, data = clean_colleges)

answer7 <- function( my_lm , color_str) 
{
  autoplot(my_lm, which = 1:2, smooth.colour = color_str,
           data = clean_colleges, colour = color_str) +
    theme_bw()
}

但它抛出的错误:当我打电话型LM的对象不自动绘制支持answer7(demo_lm, "#34925E")

Answer 1:

这里的东西,让你开始。 从错误信息autoplot清楚地表明,它不能处理lm的对象。 只需提取残差和拟合值,那么情节:

answer7 <- function(my_lm, color_str){
    resids <- residuals(my_lm)
    fitted <- fitted(my_lm)

   p_vs_r <- ggplot(data = NULL, aes(x = fitted, y = resids))+
        geom_point(colour = color_str)

   qq_r <- ggplot(data = NULL, aes(sample = resids))+
        stat_qq(colour = color_str)

   plot(p_vs_r)
   plot(qq_r)

}

这可能是有用的看看了multiplot在功能Rmisc如果你想在同一设备上两条曲线包。



文章来源: Error: Objects of type lm not supported by autoplot
标签: r ggplot2