簡體   English   中英

使用 R 可視化多元線性回歸

[英]Visualizing multiple linear regression using R

我正在嘗試使用四個變量來擬合和可視化多元線性回歸 model。

fit <- lm(general_data$Bacterial.Contigs ~ general_data$Soil.Temperature + general_data$Time.point + general_data$Year)

這里,Time.point 和 Year 是分類變量,其他是數值變量。

我使用以下代碼創建了 3D plot。

library(plotly)

plot_ly(data = general_data, z = ~Bacterial.Contigs, x = ~Soil.Temperature, y = ~Time.point, color = ~Year, colors = c('#0C4B8E' ,'#BF382A'),opacity = 0.5) %>%
  add_markers( marker = list(size = 4))

plot 看起來像這樣:

在此處輸入圖像描述

如何在此 plot 中添加“適合”model 的回歸線。 我真的很感激任何幫助。 謝謝

您安裝的 model 僅具有加性效應,這意味着您的分類值只會增加或減少您的響應變量,不同類別的斜率不會改變。 在 3D plot 上很難想象,我建議你試試ggplot2

mtcars ,您基本上將擬合值放回數據框中,並為擬合值調用一行:

dat = mtcars
dat$am = factor(dat$am)
dat$vs = factor(dat$vs)

fit <- lm(mpg ~ disp + am + vs,data=dat)
dat$fitted= fitted(fit)

library(ggplo2)
g = ggplot(dat) + geom_point(aes(x=disp,y=mpg)) + 
geom_line(aes(x=disp,y = fitted)) + facet_grid(am~vs)
print(g)

在此處輸入圖像描述

或者,如果您需要 plotly:

library(plotly)
ggplotly(g)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM