【问题标题】:How to create prediction line for Quadratic Model如何为二次模型创建预测线
【发布时间】:2018-12-29 22:42:06
【问题描述】:

我正在尝试为二次模型创建二次预测线。我正在使用 R 附带的自动数据集。为线性模型创建预测线没有问题。然而,二次模型会产生看起来很疯狂的线条。这是我的代码。

# Linear Model
plot(Auto$horsepower, Auto$mpg,
     main = "MPG versus Horsepower",
     pch = 20)

lin_mod = lm(mpg ~ horsepower,
             data = Auto)
lin_pred = predict(lin_mod)


lines(
  Auto$horsepower, lin_pred,
  col = "blue", lwd = 2
)


# The Quadratic model
Auto$horsepower2 = Auto$horsepower^2
quad_model = lm(mpg ~ horsepower2,
                data = Auto)
quad_pred = predict(quad_model)

lines(
  Auto$horsepower,
  quad_pred,
  col = "red", lwd = 2
)

我 99% 确定问题出在预测函数上。为什么我不能生成一个整洁的二次预测曲线?我尝试的以下代码不起作用 - 它可能相关吗?:

quad_pred = predict(quad_model, data.frame(horsepower = Auto$horsepower))

谢谢!

【问题讨论】:

    标签: r regression predict quadratic-curve


    【解决方案1】:

    或者,使用ggplot2:

    ggplot(Auto, aes(x = horsepower, y = mpg)) + geom_point() +
              stat_smooth(aes(x = horsepower, y = mpg), method = "lm", formula = y ~ x, colour = "red") +
              stat_smooth(aes(x = horsepower, y = mpg), method = "lm", formula = y ~ poly(x, 2), colour = "blue")
    

    【讨论】:

    • 谢谢!我很欣赏在 ggplot 上展示另一种方法。它看起来很整洁。我真的很喜欢预测区间带。
    【解决方案2】:

    一个选项是创建您想要为其绘制拟合线的 x 值序列。如果您的数据存在“间隙”或者您希望绘制 x 变量范围之外的拟合线,这将很有用。

    # load dataset; if necessary run install.packages("ISLR")
    data(Auto, package = "ISLR")
    
    # since only 2 variables at issue, use short names
    mpg <- Auto$mpg
    hp  <- Auto$horsepower
    
    # fit linear and quadratic models
    lmod <- lm(mpg ~ hp)
    qmod <- lm(mpg ~ hp + I(hp^2))
    
    # plot the data
    plot(x=hp, y=mpg, pch=20)
    
    # use predict() to find coordinates of points to plot
    x_coords <- seq(from=floor(min(hp)), to=ceiling(max(hp)), by=1)
    y_coords_lmod <- predict(lmod, newdata=data.frame(hp=x_coords))
    y_coords_qmod <- predict(qmod, newdata=data.frame(hp=x_coords))
    
    # alternatively, calculate this manually using the fitted coefficients
    y_coords_lmod <- coef(lmod)[1] + coef(lmod)[2]*x_coords
    y_coords_qmod <- coef(qmod)[1] + coef(qmod)[2]*x_coords + coef(qmod)[3]*x_coords^2
    
    # add the fitted lines to the plot
    points(x=x_coords, y=y_coords_lmod, type="l", col="blue")
    points(x=x_coords, y=y_coords_qmod, type="l", col="red")
    

    【讨论】:

    • 谢谢!这看起来很直观。我喜欢您手动创建序列的方式。我认为这将有助于平滑预测线/曲线。
    【解决方案3】:

    问题是x-axis 值未排序。是否是线性模型无关紧要,但如果它是多项式模型就会很明显。我创建了一个新的排序数据集,它工作正常:

    library(ISLR) # To load data Auto
    
    # Linear Model
    plot(Auto$horsepower, Auto$mpg,
         main = "MPG versus Horsepower",
         pch = 20)
    
    lin_mod = lm(mpg ~ horsepower,
                 data = Auto)
    lin_pred = predict(lin_mod)
    
    
    lines(
      Auto$horsepower, lin_pred,
      col = "blue", lwd = 2
    )
    
    
    # The Quadratic model
    Auto$horsepower2 = Auto$horsepower^2
    
    # Sorting Auto by horsepower2
    Auto2 <- Auto[order(Auto$horsepower2), ]
    quad_model = lm(mpg ~ horsepower2,
                    data = Auto2)
    
    
    quad_pred = predict(quad_model)
    
    
    lines(
      Auto2$horsepower,
      quad_pred,
      col = "red", lwd = 2
    )
    

    【讨论】:

    • 谢谢!看起来您创建了一个全新的数据框只是为了对平方马力值进行排序。为什么我不能在预测函数中创建排序值?例如,正如我上面提到的,当我运行以下行时出现错误:predict(quad_model, data.frame(horsepower = sort(Auto$horsepower)))。我可以发誓使用 data.frame() 函数作为预测函数中的参数将允许人们控制所需的 x 值以进行预测——因此在那里对其进行排序而不是完全创建一个 Auto2 数据框。
    • 您得到的错误很可能是Error in eval(predvars, data, env) : object 'horsepower2' not found。你应该把它改成这样:quad_pred = predict(quad_model, data.frame(horsepower2 = sort(Auto$horsepower^2)))
    猜你喜欢
    • 2018-05-18
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 2016-10-06
    • 2021-02-25
    • 2022-01-21
    相关资源
    最近更新 更多