我有一個 GAM,現在添加了國家作為一個因素,以模擬國家內部的關系(我認為這已經正確完成,如果沒有,請告訴我),現在希望繪制我的新模型,我的問題是什么是美學錯誤(見下文),如果有人知道如何正確繪制這個,那就太好了
! Aesthetics must be either length 1 or the same as the data (480): y
這是我使用的代碼和我的資料片段(讓我知道你是否需要我的整個資料集,我可以附上它),希望有人能夠繪制這個,我認為錯誤來了,因為我沒有繪制模型現在我正確地添加了國家作為一個因素
型號代碼;
mod = gam(gdp_per_capita ~ s(fisheries_production_pc, k = 25, bs = 'cs') as.factor(Country_Name),
data = economy_df,
family = gaussian(link = "log"))
#predictions
preds = predict(mod, type = 'response', se.fit = TRUE)
#plot model
plot = ggplot(economy_df, aes(y = gdp_per_capita, x = fisheries_production_pc))
geom_point()
geom_line(aes(fisheries_production_pc, preds$fit), colour = 'red')
scale_x_log10()
資料;
economy_df
Country_Name year gdp_per_capita fisheries_production
Albania 1997 717.3800 1110.80
Albania 1998 813.7894 2807.50
Albania 1999 1033.2425 3057.90
Albania 2000 1126.6833 3635.00
Albania 2001 1281.6598 3597.20
Albania 2002 1425.1242 4516.80
Bosnia 1997 982.8018 253.00
Bosnia 1998 1102.3907 254.00
Bosnia 1999 1251.7476 255.00
Bosnia 2000 1484.1761 255.00
Bosnia 2001 1544.6021 255.00
Croatia 1997 5312.3695 20551.49
Croatia 1998 5691.1095 27935.08
Croatia 1999 5246.9360 25222.19
Croatia 2000 4887.7137 27944.24
Croatia 2001 5412.9251 29019.12
Cyprus 1997 14234.2441 25788.00
Cyprus 1998 15092.8262 20482.00
Cyprus 1999 15287.9189 41060.00
Cyprus 2000 14388.3477 70223.00
uj5u.com熱心網友回復:
使用該emmeans包生成預測更容易一些,特別是如果您想對非焦點預測變數進行平均(邊緣化)(例如,從具有國家作為固定效應的模型生成與國家無關的預測);您也可以ggpredict為此使用包(包裝emmeans包)
library(mgcv)
library(emmeans)
library(ggplot2)
mod <- gam(gdp_per_capita ~ s(fisheries_production_pc) factor(Country_Name),
data = economy_df,
family = gaussian(link = "log"))
## predictions
fpcvec <- exp(seq(log(200), log(40000), length.out = 51))
predfun <- function(mod, specs) {
as.data.frame(
emmeans(mod,
specs = specs,
data = economy_df,
type = "response",
at = list(fisheries_production_pc = fpcvec))
)
}
pred1 <- predfun(mod, specs = ~fisheries_production_pc)
pred2 <- predfun(mod, specs = ~fisheries_production_pc*Country_Name)
#plot model
plot <- ggplot(economy_df,
aes(y = gdp_per_capita, x = fisheries_production_pc))
geom_point()
facet_wrap(~Country_Name)
geom_line(data = pred1, aes(y = response))
geom_line(data = pred2, aes(y = response), colour = 'red')
scale_x_log10()
ggsave("fishpred.png")
筆記:
- 在這里,我繪制了與國家無關的(邊際)預測(黑色)和特定國家的預測(紅色)
- 如果您想添加置信區域,您可以
geom_ribbon與生成的lower.CL和upper.CL變數一起使用emmeans - 由于您擬合了國家/地區的加性效應,因此特定國家/地區的預測都與整體預測/彼此平行(在對數尺度上!)。(具有特定級別平滑的模型會被這個小資料集破壞......)
- Pedersen et al 2019(參考下文)是您在擬合這種多國 GAM 時應該考慮的選擇的一個很好的參考。

Pedersen、Eric J.、David L. Miller、Gavin L. Simpson 和 Noam Ross。2019. “生態學中的分層廣義加法模型:Mgcv 簡介。” PeerJ 7(5 月):e6876。https://doi.org/10.7717/peerj.6876。
uj5u.com熱心網友回復:
我認為你可以這樣做:
ggplot(economy_df, aes(y = gdp_per_capita,
x = fisheries_production_pc,
color=Country_Name))
geom_point()
facet_wrap(~Country_Name, scales = "free")
geom_smooth(method="gam", formula=y~s(x,bs="cs"))
scale_x_log10()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/523449.html
標籤:rggplot2游戏
