在 R 統計包中,有沒有辦法用一個連續變數和一個分類變數繪制二階多項式回歸的圖形?
要生成具有一個分類變數的線性回歸圖:
library(ggplot2)
library(ggthemes) ## theme_few()
set.seed(1)
df <- data.frame(minutes = runif(60, 5, 15), endtime=60, category="a")
df$category = df$category=letters[seq( from = 1, to = 2 )]
df$endtime = df$endtime df$minutes^3/180 df$minutes*runif(60, 1, 2)
ggplot(df, aes(y=endtime, x=minutes, col = category))
geom_point()
geom_smooth(method=lm)
theme_few()
要繪制具有一個連續變數的多項式圖:
ggplot(df, aes(x=minutes, y=endtime))
geom_point()
stat_smooth(method='lm', formula = y ~ poly(x,2), size = 1)
xlab('Minutes of warm up')
ylab('End time')
但我不知道如何用一個連續變數和一個分類變數繪制多項式圖。
uj5u.com熱心網友回復:
只需添加一個colour或group映射。這將使 ggplot 擬合并為每個類別顯示單獨的多項式回歸。(1) 不可能顯示加性混合多項式回歸(即lm(y ~ poly(x,2) category));(2) 此處顯示的內容與互動模型的結果并不完全相同lm(y ~ poly(x,2)*col),因為殘差方差(以及置信帶的寬度)是針對每組單獨估計的。
ggplot(df, aes(x=minutes, y=endtime, col = category))
geom_point()
stat_smooth(method='lm', formula = y ~ poly(x,2))
labs(x = 'Minutes of warm up', y = 'End time')
theme_few()

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/352304.html
