我有一个与虹膜数据集相似的数据框:
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)
这总体上显示了哪个变量最重要。但是,我想在整个数据集以及每个物种的也上都观察到这一点(即哪个变量最能预测每个物种)。
任何帮助将不胜感激。
答案 0 :(得分:1)
运行回归之前,您可以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