我想使用二項式負廣義線性混合模型(GLMM)繪制(分類)和(數字)ladenant函式中回應變數數量之間的關系,但沒有成功。我嘗試做:Biomatemp
#Packages
library(lme4)
library(ggplot2)
library(ggeffects)
#Open my dataset
myds<-read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/my_glmm_dataset.csv")
myds <- myds[,-c(3)] # remove bad character variable
# Negative binomial GLMM
m.laden.1 <- glmer.nb(ladenant ~ Bioma poly(temp,2) scale(UR) (1 | formigueiro), data = DataBase)
# Plot the results
mydf <- ggpredict(m.laden.1, terms = c("temp","Bioma"))
ggplot(mydf, aes(x, predicted), group = Bioma)
geom_point(DataBase, aes(temp, ladenant), alpha = 0.5) # Observed ladenant response variable
geom_line()
geom_ribbon(aes(ymin = conf.low, ymax = conf.high), alpha = .1)
我有一個不太好的情節,因為我沒有每一Bioma行和temp變數的壞行:

但是物件mydf規范包含Bioma變數:
mydf
# # Predicted counts of ladenant
# # Bioma = Atlantic Forest
# temp | Predicted | 95% CI
# ---------------------------------
# 10 | 1.88 | [ 0.81, 4.35]
# 15 | 12.95 | [ 9.11, 18.40]
# 20 | 32.61 | [26.42, 40.25]
# 25 | 30.00 | [23.51, 38.28]
# 30 | 10.08 | [ 4.79, 21.24]
# 35 | 1.24 | [ 0.24, 6.43]
# # Bioma = Transition
# temp | Predicted | 95% CI
# ----------------------------------
# 10 | 6.84 | [ 3.04, 15.42]
# 15 | 47.17 | [34.05, 65.34]
# 20 | 118.79 | [92.27, 152.94]
# 25 | 109.29 | [76.84, 155.43]
# 30 | 36.73 | [16.17, 83.44]
# 35 | 4.51 | [ 0.82, 24.71]
# # Bioma = Pampa
# temp | Predicted | 95% CI
# ---------------------------------
# 10 | 1.42 | [ 0.70, 2.90]
# 15 | 9.80 | [ 7.47, 12.86]
# 20 | 24.69 | [18.74, 32.52]
# 25 | 22.71 | [16.46, 31.35]
# 30 | 7.63 | [ 3.65, 15.96]
# 35 | 0.94 | [ 0.19, 4.67]
# Adjusted for:
# * UR = 82.78
# * formigueiro = 0 (population-level)
Plaese,對改善這個情節有什么幫助嗎?
uj5u.com熱心網友回復:
我認為您只需要注意兩個物件mydsand 中變數的不同名稱mydf,以及將它們放置在對各種geoms的呼叫中的位置:
library(lme4)
#> Loading required package: Matrix
library(ggplot2)
library(ggeffects)
#Open my dataset
myds<-read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/my_glmm_dataset.csv")
myds <- myds[,-c(3)] # remove bad character variable
# Negative binomial GLMM
m.laden.1 <- glmer.nb(ladenant ~ Bioma poly(temp,2) scale(UR) (1 | formigueiro),
data = myds)
# Plot the results
mydf <- ggpredict(m.laden.1, terms = c("temp [all]", "Bioma"))
ggplot(mydf, aes(x, predicted))
geom_point(data=myds, aes(temp, ladenant, color = Bioma), alpha = 0.5)
geom_line(aes(color = group))
labs(x = "temp", y = "ladenant")

請注意,我沒有包括你的geom_ribbon,因為conf.low和conf.high都NA在曲線的上半部分,這使得它看起來很混亂。
順便說一下,該圖在 log y 尺度下可能會提供更多資訊:
ggplot(mydf, aes(x, predicted))
geom_point(data=myds, aes(temp, ladenant, color = Bioma), alpha = 0.5)
geom_line(aes(color = group))
scale_y_log10()
labs(x = "temp", y = "ladenant")

由reprex 包( v2.0.0 )于 2021 年 11 月 12 日創建
uj5u.com熱心網友回復:
您還可以使用plot(),它回傳一個 ggplot 物件,并根據需要添加其他層。
library(lme4)
#> Loading required package: Matrix
library(ggplot2)
library(ggeffects)
#Open my dataset
myds<-read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/my_glmm_dataset.csv")
myds <- myds[,-c(3)] # remove bad character variable
# Negative binomial GLMM
m.laden.1 <- glmer.nb(ladenant ~ Bioma poly(temp,2) scale(UR) (1 | formigueiro), data = myds)
mydf <- ggpredict(m.laden.1, terms = c("temp [all]","Bioma"))
plot(mydf, add.data = TRUE, ci = FALSE)

plot(mydf, add.data = TRUE, ci = FALSE) ggplot2::scale_y_log10()
#> Scale for 'y' is already present. Adding another scale for 'y', which will
#> replace the existing scale.

由reprex 包(v2.0.1)于 2021 年 11 月 17 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/358899.html
下一篇:如何在r中繪制偏斜資料和正常資料
