[英]Variable sample size per cluster/group in mixed effects logistic regression
[英]Logistic regression per group
我有一個與 iris 數據集類似的數據框:
library(datasets)
df <- iris
head(df)
我正在嘗試查看哪個列/變量最能預測每個物種。
我試過邏輯回歸:
model <- glm(Species ~
Petal.Width+
Petal.Length+
Sepal.Width+
Sepal.Length,
data=df,
family = binomial(logit))
model
summary(model)
anova(model)
這總體上向我展示了哪個變量最重要。 不過,我想觀察這對於整個數據集,也為每一個物種(即該變量最佳預測每個品種)。
任何幫助將非常感激。
您可以在運行回歸之前group_by(Species)
。 考慮以下示例:
library(datasets)
df <- iris
### models for each species
library(dplyr)
models <- df %>%
group_by(Species) %>%
do(model = glm(Species ~
Petal.Width+
Petal.Length+
Sepal.Width+
Sepal.Length,
data = .,
family = binomial(logit)))
models$model # list of the three models with their estimates
# output
[[1]]
Call: glm(formula = Species ~ Petal.Width + Petal.Length + Sepal.Width +
Sepal.Length, family = binomial(logit), data = .)
Coefficients:
(Intercept) Petal.Width Petal.Length Sepal.Width Sepal.Length
-2.657e+01 -1.391e-14 -2.777e-15 3.349e-15 1.244e-15
Degrees of Freedom: 49 Total (i.e. Null); 45 Residual
Null Deviance: 0
Residual Deviance: 2.901e-10 AIC: 10
[[2]]
Call: glm(formula = Species ~ Petal.Width + Petal.Length + Sepal.Width +
Sepal.Length, family = binomial(logit), data = .)
Coefficients:
(Intercept) Petal.Width Petal.Length Sepal.Width Sepal.Length
-2.657e+01 -6.410e-14 -1.630e-14 3.634e-14 4.943e-14
Degrees of Freedom: 49 Total (i.e. Null); 45 Residual
Null Deviance: 0
Residual Deviance: 2.901e-10 AIC: 10
[[3]]
Call: glm(formula = Species ~ Petal.Width + Petal.Length + Sepal.Width +
Sepal.Length, family = binomial(logit), data = .)
Coefficients:
(Intercept) Petal.Width Petal.Length Sepal.Width Sepal.Length
-2.657e+01 1.135e-15 1.749e-14 -6.585e-15 -9.822e-15
Degrees of Freedom: 49 Total (i.e. Null); 45 Residual
Null Deviance: 0
Residual Deviance: 2.901e-10 AIC: 10
### to get std.error, p.value, etc.
library(broom)
models %>% tidy(model)
# output
# A tibble: 15 x 6
# Groups: Species [3]
Species term estimate std.error statistic p.value
<fctr> <chr> <dbl> <dbl> <dbl> <dbl>
1 setosa (Intercept) -2.656607e+01 786965.7 -3.375760e-05 0.9999731
2 setosa Petal.Width -1.390512e-14 523902.1 -2.654145e-20 1.0000000
3 setosa Petal.Length -2.776770e-15 316800.5 -8.765044e-21 1.0000000
4 setosa Sepal.Width 3.349353e-15 200749.8 1.668422e-20 1.0000000
5 setosa Sepal.Length 1.243936e-15 221428.0 5.617790e-21 1.0000000
6 versicolor (Intercept) -2.656607e+01 615841.5 -4.313783e-05 0.9999656
7 versicolor Petal.Width -6.410448e-14 475196.2 -1.349011e-19 1.0000000
8 versicolor Petal.Length -1.629537e-14 226400.3 -7.197591e-20 1.0000000
9 versicolor Sepal.Width 3.634246e-14 225869.3 1.609004e-19 1.0000000
10 versicolor Sepal.Length 4.943463e-14 156829.6 3.152123e-19 1.0000000
11 virginica (Intercept) -2.656607e+01 608647.0 -4.364774e-05 0.9999652
12 virginica Petal.Width 1.135375e-15 223583.6 5.078078e-21 1.0000000
13 virginica Petal.Length 1.748522e-14 186273.0 9.386880e-20 1.0000000
14 virginica Sepal.Width -6.584951e-15 202705.8 -3.248527e-20 1.0000000
15 virginica Sepal.Length -9.821934e-15 165119.0 -5.948396e-20 1.0000000
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.