Quadratic regression line using R plotly

两盒软妹~` 提交于 2020-12-11 08:46:34

问题


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))

and then a plotly with

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

producing this plot:

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))*

gives me:

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:

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)

Thank you very much in advance.


回答1:


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)

With data from the comment:

 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.




回答2:


You could also use ggplot2 and ggplotly to get what you want. Try this:

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)


来源:https://stackoverflow.com/questions/49490886/quadratic-regression-line-using-r-plotly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!