【问题标题】:geom_smooth custom linear modelgeom_smooth 自定义线性模型
【发布时间】:2017-11-30 01:29:32
【问题描述】:

查看this 问题时,我无法为geom_smooth 指定自定义线性模型。我的代码如下:

example.label <- c("A","A","A","A","A","B","B","B","B","B")
example.value <- c(5, 4, 4, 5, 3, 8, 9, 11, 10, 9)
example.age <- c(30, 40, 50, 60, 70, 30, 40, 50, 60, 70)
example.score <- c(90,95,89,91,85,83,88,94,83,90)
example.data <- data.frame(example.label, example.value,example.age,example.score)

p = ggplot(example.data, aes(x=example.age,
                         y=example.value,color=example.label)) +
  geom_point()
  #geom_smooth(method = lm)

cf = function(dt){
  lm(example.value ~example.age+example.score, data = dt)
}

cf(example.data)

p_smooth <- by(example.data, example.data$example.label, 
               function(x) geom_smooth(data=x, method = lm, formula = cf(x)))

p + p_smooth 

我收到此错误/警告:

Warning messages:
1: Computation failed in `stat_smooth()`:
object 'weight' not found 
2: Computation failed in `stat_smooth()`:
object 'weight' not found 

为什么我会得到这个?将自定义模型指定为geom_smooth 的正确方法是什么?谢谢。

【问题讨论】:

  • 我认为geom_smooth的模型公式只能是y ~ f(x)的形式(例如y ~ x,这是默认的,或者y ~ poly(x, 2)y ~ bs(x, df=4)等) .该公式只能包含 x 和 y,它们将代表您传递给 aes 的任何数据列作为绘图的 x 和 y 美学。如果要绘制多元回归的结果,可以构建一个数据框以提供给predict,然后使用例如geom_line 绘制输出。
  • 您是否值得尝试重塑数据,然后为xsubset 函数使用单列?
  • 这不是真正的重塑问题。你真正可以用geom_smooth 做的只是情节回归,包括用于你的 y 和 x 美学的变量以及任何分类美学(例如,颜色、形状)。例如,在您的情况下,您可以执行 p + geom_smooth(method=lm, formula=y ~ poly(x, 2))p + geom_smooth(method=lm, formula=y ~ splines::bs(x, df=4)) (尽管您需要更多数据才能使其有意义)。这些将为example.label 的每个级别提供单独的回归线。
  • 例如,对于mtcars 数据框,这里是mpgwt 的回归线,但按vsam 的级别分类:ggplot(mtcars, aes(wt, mpg, colour=interaction(vs, am, sep="_"))) + geom_point() + geom_smooth(se=FALSE, method="lm")
  • 我明白了,谢谢。看起来没有解决方法...

标签: r ggplot2 regression lm


【解决方案1】:

具有两个连续预测变量和一个连续结果的回归模型的回归函数存在于 3D 空间中(两个用于预测变量,一个用于结果),而 ggplot 图是一个 2D 空间(一个连续预测变量在x 轴和 y 轴上的结果)。这就是为什么您无法使用 geom_smooth 绘制两个连续预测变量的函数的根本原因。

一种“解决方法”是选择一个连续预测变量的几个特定值,然后为第一个变量的每个选定值在 x 轴上为另一个连续预测变量绘制一条线。

以下是 mtcars 数据框的示例。下面的回归模型使用wthp 预测mpg。然后,我们绘制mpgwthp 的各种值的预测。我们创建一个预测数据框,然后使用geom_line 进行绘图。图中的每条线代表mpgwthp 的不同值的回归预测。当然,您也可以颠倒wthp 的角色。

library(ggplot)
theme_set(theme_classic())

d = mtcars
m2 = lm(mpg ~ wt + hp, data=d)

pred.data = expand.grid(wt = seq(min(d$wt), max(d$wt), length=20),
                        hp = quantile(d$hp))
pred.data$mpg = predict(m2, newdata=pred.data)

ggplot(pred.data, aes(wt, mpg, colour=factor(hp))) +
  geom_line() +
  labs(colour="HP Quantiles")

另一种选择是使用颜色渐变来表示 mpg(结果)并在 x 和 y 轴上绘制 wthp

pred.data = expand.grid(wt = seq(min(d$wt), max(d$wt), length=100),
                        hp = seq(min(d$hp), max(d$hp), length=100))
pred.data$mpg = predict(m2, newdata=pred.data)

ggplot(pred.data, aes(wt, hp, z=mpg, fill=mpg)) +
  geom_tile() +
  scale_fill_gradient2(low="red", mid="yellow", high="blue", midpoint=median(pred.data$mpg)) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多