我对 R 很陌生,对 plotly 也很陌生.
I am quite new to R and really new in plotly.
我正在尝试绘制二次(即二阶多项式)回归线.一旦某些价格与年份,以及一旦相同的价格与某些整数列表(可以相同),我们就说分数.本例中的数据为
I am trying to plot a quadratic (i.e. 2nd-degree polynomial) regression line. Once some prices vs years, and once the same prices vs a list of certain integer numbers (which can be the same), let's say scores. The data in this example are
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)通过编码,第一次拟合效果很好
The first fit works well by coding
enter code here qfit1 <- lm(price ~ poly (year,2))然后一个情节与
add_trace(x=year, y=fitted(qfit1), type="scatter", mode="lines", line=list(shape="spline"),)制作这个情节:
但是,第二次拟合不起作用:
However, the second fit doesn't work:
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 个值的行连续
which links the 12 data values I have by curve lines. I then ordered the data so that the line that links the 12 values would be continuous by
add_trace(x=sort(score), y=fitted(qfit2)[order(score)], type="scatter", mode="lines", line=list(shape="spline", smoothing=1.3))*但结果又不是我想要的:
but again the result is not what I want:
产生的线一点也不平滑,它基本上将12个值与曲线联系起来,我注意到(当然我用不同的数据制作了更多相似的图表)问题总是发生在某个分数(x-axis) 有不同的价格.但是,我无法理解如何解决这个问题.对此有什么想法吗?或者也许有人知道使用 R 和 plotly 生成二次拟合线的不同方法?(我也尝试使用 add_lines 而不是 add_trace,但这给了我更糟糕的结果)
The line produced is not smooth at all, it basically links the 12 values with curve lines, and what I noticed (of course I produced more similar graphs with different data) was that the problem always happens when a certain score (x-axis) has various prices. However, I can't understand how to solve that. Any idea on that? Or maybe anyone knowing a different way of producing a quadratic fit line using R and plotly? (I also tried to use add_lines instead of add_trace, but that gave me an even worse result)
非常感谢您.
推荐答案以下是在 plotly 中绘制拟合模型的工作代码:
Here is a working code to plot a fitted model in 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)这条线不太平滑,因为拟合点很少.要使线条更平滑,请创建新数据:
the line is not so smooth since there are few fitted points. To make a smoother line create new data:
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)问题是您的拟合值稀疏且不均匀,因此您需要对均匀分布的新数据进行预测以获得漂亮的曲线.
Problem is your fitted values are sparse and uneven, so you need to predict on new data that is evenly spaced to get a nice looking curve.
更多推荐
使用 R plotly 的二次回归线
发布评论