【发布时间】: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