R Plotly - 绘制多个多项式回归线

R Plotly - Plotting Multiple Polynomial Regression Lines

我有一个类似于下面的图表:

library(plotly)
df <-  as.data.frame(1:19)

df$CATEGORY <- c("C","C","A","A","A","B","B","A","B","B","A","C","B","B","A","B","C","B","B")
df$x <- c(126,40,12,42,17,150,54,35,21,71,52,115,52,40,22,73,98,35,196)
df$y <- c(92,62,4,23,60,60,49,41,50,76,52,24,9,78,71,25,21,22,25)

df[,1] <- NULL

df$fv <- df %>%
  filter(!is.na(x)) %>%
  lm(y ~ x*CATEGORY,.) %>%
  fitted.values()

p <- plot_ly(data = df,
         x = ~x,
         y = ~y,
         color = ~CATEGORY,
         type = "scatter",
         mode = "markers"
) %>%
  add_trace(x = ~x, y = ~fv, mode = "lines")

p

它工作正常,因为我需要在同一个图上有多个回归线,但我真正需要的是每个类别的多项式回归线。 我试图用以下内容替换“lm(y ~ x*CATEGORY,.)”:

df1$fv <- df1 %>%
                                                  filter(!is.na(x)) %>%
                                                  lm(y ~ poly(x*CATEGORY,.),2) %>%
                                                  fitted.values()

但它不起作用。 有什么建议么? 谢谢

您的公式不正确。尝试:

df %>%
   filter(!is.na(x)) %>%
   lm(y ~ poly(x,2, raw=TRUE)*CATEGORY, data=.) %>%
   fitted.values()
  • df1 未在您的示例代码中定义,因此此处假设为 df。
  • 使用数据=。引用数据源。
  • 在 poly 函数中定义幂,在本例中为 2。
  • 将 CATEGORY 移出 poly 函数。 x* 类别 + x^2* 类别等

@Dave2e 关于 mis-specification 的说法是正确的。要生成看起来不错的图(即生成 polynomial-looking 曲线的图,您可能需要做更多的工作:

library(plotly)
library(ggeffects)
library(dplyr)
library(tidyr)
df <-  as.data.frame(1:19)

df$CATEGORY <- c("C","C","A","A","A","B","B","A","B","B","A","C","B","B","A","B","C","B","B")
df$x <- c(126,40,12,42,17,150,54,35,21,71,52,115,52,40,22,73,98,35,196)
df$y <- c(92,62,4,23,60,60,49,41,50,76,52,24,9,78,71,25,21,22,25)

df[,1] <- NULL

df <- df %>% arrange(CATEGORY, x)

hyp <- by(df$x, list(df$CATEGORY), function(x)seq(min(x), max(x), length=50))

hyp <- do.call(data.frame, hyp) %>% 
  pivot_longer(everything(), names_to = "CATEGORY", values_to="x")

mod <- lm(y ~ poly(x, 2)*CATEGORY, data=df)
hyp$predicted <- predict(mod, newdata=hyp)
 

p <- plot_ly() %>%
  add_trace(data = df,
            x = ~x,
            y = ~y,
            color = ~CATEGORY,
            type = "scatter",
            mode = "markers") %>%
  add_trace(data = hyp, x = ~x, y = ~predicted, color = ~CATEGORY, mode = "lines")
p
#> No trace type specified:
#>   Based on info supplied, a 'scatter' trace seems appropriate.
#>   Read more about this trace type -> https://plotly.com/r/reference/#scatter

reprex package (v2.0.1)

于 2022-04-07 创建