错误:自动绘图不支持lm类型的对象 [英] Error: Objects of type lm not supported by autoplot

查看:895
本文介绍了错误:自动绘图不支持lm类型的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

一个线性模型和一个与颜色对应的字符串,

>

它应该为这个线性模型创建一个Residuals vs Fitted和QQ图,并根据像这样的字符串参数着色点:$ b​​
$ b


demo_lm <-lm(ExpenditurePerStud_Status,data = clean_colleges)

answer7(demo_lm,#34925E)


这是我试过的:

  
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,color = color_str)+
theme_bw()
}

但是当我调用 an时,抛出Error:类型为lm的对象不被autoplot支持swer7(demo_lm,#34925E)

解决方案

来自 autoplot 的错误消息清楚地表明它不能处理 lm 对象。只需提取残差和拟合值,然后绘制:

  answer7 < -  function(my_lm,color_str){
(my_lm)

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

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

plot(p_vs_r)
plot(qq_r)

}

Rmisc 中查看 multiplot strong>包,如果你想在同一台设备上使用这两个图。


I am trying to create a simple function that accepts two arguments:

A linear model and a string that corresponds to a color, in that order.

It should create a Residuals vs Fitted and Q-Q plot for this linear model, and color the points according to the string argument like this:

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

answer7(demo_lm, "#34925E")

This is what I have tried:

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()
}

But it throws the Error: Objects of type lm not supported by autoplot when I call answer7(demo_lm, "#34925E")

解决方案

Here's something to get you started. The error message from autoplot clearly indicates that it cannot handle lm objects. Just extract the residuals and fitted values, then plot:

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)

}

It may be useful to take a look at the multiplot function in the Rmisc package if you want both plots on the same device.

这篇关于错误:自动绘图不支持lm类型的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆