[英]How to get the vertex value of quadratic term in GLM in R?
我在 GLM 中有一個二次項,我對二次項的頂點值(+ 頂點的標准誤差和置信區間)感興趣。 據我所知,在 R 中沒有用於此目的的自動 function,我無法手動計算,因為我沒有足夠的統計知識和 ZE1E1D3D40573127E9EE0480CAF1283D。 有沒有人可以幫助並構建代碼以獲取感興趣的值?
GLM <- glm(Y ~ X1 + I(X1^2) + X2 + X3 + X4 + X5, family="binomial", data=DF)
除了警告,因為我不習慣為這些類型的問題模擬數據。 這是一個基於 Bonferroni CI 參數的帶有 CI 的頂點。 由於我們需要兩個參數,我們需要調整兩個 CI 的乘積,使得x^2=1-alpha
->
x=(1-alpha)^(1/2)
set.seed(1)
library(dplyr, warn.conflicts = F)
library(ggplot2, warn.conflicts = F)
logit_trans <- function(x) 1/(1/exp(x)+1)
sim_data <- tibble(
x = rep(seq(0,20,.1), 10),
y = round(logit_trans(20 - 8*x + .4*x^2 + rnorm(2010, 0, 1)))
)
model <- glm(
y ~ x + I(x^2),
binomial(),
sim_data
)
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
find_vertex <- function(model_pars){
a <- model_pars[2]
b <- model_pars[3]
if(2*b>0){ #Check second deriv.
-a/(2*b)
} else{ # concave down
NA
}
}
ci_vertex <- function(model){
mean_vertex <- model %>%
coef() %>%
find_vertex()
ci <- model %>%
confint(parm = 2:3, level = (1-.05)^(1/2))
lower_ci <- ci %>%
magrittr::extract(,1) %>%
c(0, .) %>%
find_vertex()
upper_ci <- ci %>%
magrittr::extract(,2) %>%
c(0, .) %>%
find_vertex()
list(
lower_ci = lower_ci,
mean_vertex = mean_vertex,
upper_ci = upper_ci
)
}
ci <- ci_vertex(model)
#> Waiting for profiling to be done...
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
sim_data %>%
mutate(
x2 = x^2,
y_expected = logit_trans(c(matrix(c(rep(1, n()), x, x2), ncol = 3) %*% matrix(coef(model))))
) %>%
ggplot(aes(x, y_expected)) +
geom_point() +
geom_line() +
geom_vline(xintercept = ci$lower_ci, color = 'red', linetype = 'dashed') +
geom_vline(xintercept = ci$upper_ci, color = 'red', linetype = 'dashed') +
geom_vline(xintercept = ci$mean_vertex, color = 'red')
由reprex package (v2.0.1) 於 2022 年 8 月 19 日創建
正如我們對y
的輸入 function 所期望的那樣,平均頂點幾乎正好是10
。 由於某種原因,上下 CI 是倒置的,下一個比預期的稍大。 我猜這是由於 model 無法識別。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.