[英]Weird output of tab_model() with glmmTMB
當我使用glmmTMB
包的tab_model()
函數與sjPlot
包的glmmTMB
函數來擬合具有 beta 系列響應的廣義線性混合模型時,我得到了一個奇怪的輸出。 截距和邊際 R² 看起來很奇怪。
這里發生了什么?
df <- structure(list(date = structure(c(6L, 5L, 6L, 1L, 4L, 2L, 2L,
2L, 2L, 4L, 6L, 1L, 6L, 6L, 2L, 2L, 4L, 4L, 5L, 1L), .Label = c("2021-03-17",
"2021-04-07", "2021-04-13", "2021-04-27", "2021-05-11", "2021-05-27"
), class = "factor"), kettlehole = structure(c(4L, 6L, 6L, 4L,
7L, 2L, 6L, 5L, 3L, 5L, 1L, 1L, 1L, 1L, 4L, 4L, 5L, 4L, 3L, 5L
), .Label = c("1189", "119", "1202", "149", "172", "2484", "552"
), class = "factor"), plot = structure(c(8L, 4L, 4L, 3L, 7L,
8L, 1L, 3L, 6L, 4L, 4L, 3L, 6L, 1L, 2L, 7L, 5L, 8L, 1L, 1L), .Label = c("1",
"2", "3", "4", "5", "6", "7", "8"), class = "factor"), treatment = structure(c(2L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
1L, 2L, 1L), .Label = c("a", "b"), class = "factor"), distance = structure(c(2L,
2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L,
2L, 1L, 1L), .Label = c("2", "5"), class = "factor"), soil_moisture_content = c(0.2173,
0.1028, 0.148, 0.3852, 0.1535, 0.2618, 0.2295, 0.222, 0.3145,
0.1482, 0.2442, 0.3225, 0.1715, 0.1598, 0.2358, 0.274, 0.1543,
0.144, 0.128, 0.361), yield = c(0.518, 0.434, 0.35, 0.599, 0.594,
0.73, 0.568, 0.442, 0.695, 0.73, 0.667, 0.49, 0.744, 0.56, 0.485,
0.532, 0.668, 0.511, 0.555, 0.718), weed_coverage = c(0, 0.045,
0.03, 0.002, 0.11, 0.003, 0.01, 0, 0.02, 0.002, 0, 0.008, 0,
0.002, 0, 0.006, 0, 0, 0.02, 0.002)), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
library(sjPlot)
library(glmmTMB)
glmmTMB(yield ~ soil_moisture_content + weed_coverage + distance + treatment + (1/kettlehole/plot) + (1|date), family = "beta_family", data = df) -> modop
tab_model(modop)
編輯
所以這是我在 n=630 的實際數據集上使用的 tab_model() 結果的屏幕截圖。 我認為問題在於模型過擬合,正如 Ben 所提到的,需要通過消除不必要的預測變量來進行調整。
tl;博士奇怪的攔截結果似乎是sjPlot::tab_model
中的一個錯誤,應該在sjPlot 問題列表中向維護人員報告 - 似乎tab_model
在不應該時錯誤地對分散參數求冪。 但是,您的模型還有其他問題(可能是過度擬合),這些問題會破壞您的邊際 R^2 值。
以下是一些合理的模擬數據,顯示了tab_model()
的問題:
set.seed(101)
## rbeta() function parameterized by mean and shape
my_rbeta <- function(n, mu, shape0) {
rbeta(n, shape1 = mu*shape0, shape2 = (1-mu)*shape0)
}
n <- 100; ng <- 10
dd <- data.frame(x = rnorm(n),
f = factor(rep(1:(n/ng), ng)))
dd <- transform(dd,
y = my_rbeta(n,
mu = plogis(-1 + 2*x + rnorm(ng)[f]),
shape0 = 5))
m1 <- glmmTMB(y ~ x + (1|f), family = "beta_family", dd)
tab_model(m1)
sigma(m1)
、 print(m1)
、 summary(m1)
的結果都同意估計的色散參數為 5.56(接近其標稱值 5),並且與confint(m1, "disp_")
一致:
2.5 % 97.5 % Estimate
sigma 4.068351 7.606602 5.562942
但是, tab_model()
報告:
顯示兩個問題:
exp(5.563) = 260.6
,置信區間同樣(不正確)取冪(Intercept)
,這令人困惑(從技術上講,它是色散模型的“截距”)然而,R^2 值看起來很合理——我們會回到這個。
模型本身呢?
一個合理的經驗法則(參見例如 Harrell Regression Modeling Strategies )說您通常應該針對每 10-20 個觀測值設置大約 1 個參數。 在固定效應和隨機效應之間,您有 20 個觀測值的 9 個參數( length(modop$fit$par)
或nobs(modop) - df.residual(modop)
)。
如果我們運行diagnose(modop)
(注意我使用的是固定/開發版本的 diagnostic diagnose()
,您的結果可能會略有不同)給出:
diagnose(modop)
Unusually large coefficients (|x|>10):
theta_1|date.1
-11.77722
zi 中的大負系數(零通脹的對數幾率)、分散或隨機效應(對數標准偏差)表明不必要的成分(在約束尺度上收斂到零)......
(如果您查看summary(modop)
,您會發現date
隨機效應的估計標准偏差為 7e-6,比下一個最大的隨機效應項小約 4 個數量級......)
modop2 <- update(modop, . ~ . - (1|date))
diagnose(modop2)
說這個模型是好的。
然而, tab_model(modop2)
仍然給出了一個可疑的條件 R^2(1.038,即 >1)。 直接運行performance::r2_nakagawa(modop2)
(我相信這是tab_model()
使用的底層機制)給出:
# R2 for Mixed Models
Conditional R2: 1.038
Marginal R2: 0.183
但有警告
1:1.5 的 mu 太接近於零,隨機效應方差的估計可能不可靠。
2:模型的特定分布方差為負。 結果不可靠。
我基本上會得出結論,這個數據集有點太小/模型太大而無法獲得有用的 R^2 值。
FWIW 我有點擔心tab_model()
報告N_plot = 8
這個模型:它應該報告N_{plot:kettlehole} = 18
summary(modop2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.