【问题标题】:Quadratic regression line using R plotly使用 R 绘制二次回归线
【发布时间】:2018-09-04 13:32:06
【问题描述】:

我对 R 很陌生,对 plotly 也很陌生。

我正在尝试绘制二次(即二次多项式)回归线。 一旦一些价格与年份,一旦相同的价格与某些整数列表(可以相同),让我们说分数。 本例中的数据为

price = c(995, 675, 690, 600, 612, 700, 589, 532, 448, 512, 537, 560)
score = c(89, 91, 88, 89, 91, 91, 89, 93, 83, 91, 91, 90)
year = c(2005:2016)

第一个适合通过编码很好地工作

enter code here
qfit1 <- lm(price ~ poly (year,2))

然后是一个情节

add_trace(x=year, y=fitted(qfit1), type="scatter", 
          mode="lines", line=list(shape="spline"),)

制作这个情节:

但是,第二次拟合不起作用:

qfit2 <- lm(price ~ poly (score,2))
p <- plot_ly() %>% ...
  add_trace(x=score, y=fitted(qfit2), type="scatter", mode="lines", 
  line=list(shape="spline", smoothing=1.3))*

给我:

它通过曲线连接我拥有的 12 个数据值。 然后我对数据进行了排序,以便链接 12 个值的线连续

add_trace(x=sort(score), y=fitted(qfit2)[order(score)], 
          type="scatter", mode="lines", 
          line=list(shape="spline", smoothing=1.3))*

但结果又不是我想要的:

产生的线一点都不平滑,它基本上是把12个值用曲线连接起来,而我注意到(当然我用不同的数据制作了更多相似的图表)是问题总是发生在某个分数(x -axis) 有不同的价格。但是,我不明白如何解决这个问题。 对此有任何想法吗? 或者也许有人知道使用 R 和 plotly 生成二次拟合线的不同方法? (我也尝试使用 add_lines 而不是 add_trace,但这给了我更糟糕的结果)

非常感谢您。

【问题讨论】:

  • 您能否提供与代码一起使用的数据。最好使用dput,并将其粘贴到您的问题中。
  • 对于这个特定的例子,我有:price = c(995, 675, 690, 600, 612, 700, 589, 532, 448, 512, 537, 560), score = c(89 , 91, 88, 89, 91, 91, 89, 93, 83, 91, 91, 90) 和年 = c(2005:2016)。这有帮助吗?
  • 谢谢,添加答案,请检查。您能否使用 edit 将这些数据放入原始帖子中。

标签: r regression plotly quadratic r-plotly


【解决方案1】:

这是一个在 plotly 中绘制拟合模型的工作代码:

  library(plotly)
  library(dplyr)
  data(cars, package = "datasets")

 qfit1 <- lm(dist ~ poly(speed,2), data = cars)

  cars %>%     
  plot_ly() %>%  
  add_lines(x = ~speed, y = fitted(qfit1)) %>%
  add_trace(x=~speed, y=~dist)

由于拟合点很少,这条线不太平滑。要使线条更平滑,请创建​​新数据:

  dat <- data.frame(speed = (1:300)/10,
                    dist = predict(qfit1, data.frame(speed = (1:300)/10)))
  plot_ly() %>% 
      add_trace(x=~speed, y=~dist, type="scatter", mode="lines", data = dat) %>%
      add_trace(x=~speed, y=~dist, type="scatter", data = cars)

来自评论的数据:

 dat1 = data.frame(
       price = c(995, 675, 690, 600, 612, 700, 589, 532, 448, 512, 537, 560),
       score = c(89, 91, 88, 89, 91, 91, 89, 93, 83, 91, 91, 90))

qfit2 <- lm(price ~ poly (score,2), data = dat1)

  dat3 <- data.frame(score = (800:950)/10,
                    price = predict(qfit2, data.frame(score = (800:950)/10)))

plot_ly() %>% 
   add_trace(x=~score, y=~price, type="scatter", mode="lines", data = dat3) %>%
   add_trace(x=~score, y=~price, type="scatter", data = dat1)

问题是您的拟合值稀疏且不均匀,因此您需要对间隔均匀的新数据进行预测以获得漂亮的曲线。

【讨论】:

    【解决方案2】:

    您也可以使用ggplot2ggplotly 来获得您想要的。试试这个:

    library(plotly)
    library(ggplot2)
    data_df <- data.frame(price, score, year)
    p <- ggplot(data_df, aes(x=score, y=price)) + 
      geom_point() + 
      geom_smooth(method="lm", se=FALSE, fill=NA, formula=y ~ poly(x, 2, raw=TRUE),colour="blue") + 
      theme_bw() 
    ggplotly(p)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-09
      • 2017-02-05
      • 1970-01-01
      • 2020-05-08
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      相关资源
      最近更新 更多