如何过滤 glm 系数的名称?
How do I filter on the name of the glm coefficient?
以下代码
df <- data.frame(place = c("South","South","North","East"),
temperature = c(30,30,20,12),
outlookfine=c(TRUE,TRUE,FALSE,FALSE)
)
glm.fit <- glm(outlookfine ~ .,df , family= binomial)
coef.glm <-coef(summary(glm.fit))
coef.glm
产出
Estimate Std. Error z value Pr(>|z|)
(Intercept) -23.56607 79462.00 -0.0002965703 0.9997634
placeNorth 0.00000 112376.25 0.0000000000 1.0000000
placeSouth 47.13214 97320.68 0.0004842972 0.9996136
我想重新显示没有拦截且没有包含短语“South”的地方的列表
我想尝试命名索引列,然后对其进行子集化,但没有成功。
[更新]
我添加了更多数据来理解为什么 George Sava 的回答也去掉了“北”
df <- data.frame(place = c("South","South","North","East","West"),
temperature = c(30,30,20,12,15),
outlookfine=c(TRUE,TRUE,FALSE,FALSE,TRUE)
)
glm.fit <- glm(outlookfine ~ .,df, family= binomial )
coef.glm <-coef(summary(glm.fit))
coef.glm[!grepl(pattern = ("South|Intercept"), rownames(coef.glm)),]
产出
Estimate Std. Error z value Pr(>|z|)
placeNorth 3.970197e-14 185277.1 2.142843e-19 1.0000000
placeWest 4.913214e+01 185277.2 2.651818e-04 0.9997884
如果您想将行名称保留为一列,那么您可以这样做:
library(tibble)
library(dplyr)
as.data.frame(coef.glm) %>%
rownames_to_column("x") %>%
filter(!grepl("Intercept|South", x))
输出
x Estimate Std. Error t value Pr(>|t|)
1 placeNorth -1.281975e-16 3.140185e-16 -0.4082483 0.7532483
要仅保留匹配(或不匹配)特定模式的行,您可以使用:
coef.glm[!grepl("South|Intercept", rownames(coef.glm)),]
请注意,当只有一行被选中时,这将成为一个向量。
以下代码
df <- data.frame(place = c("South","South","North","East"),
temperature = c(30,30,20,12),
outlookfine=c(TRUE,TRUE,FALSE,FALSE)
)
glm.fit <- glm(outlookfine ~ .,df , family= binomial)
coef.glm <-coef(summary(glm.fit))
coef.glm
产出
Estimate Std. Error z value Pr(>|z|)
(Intercept) -23.56607 79462.00 -0.0002965703 0.9997634
placeNorth 0.00000 112376.25 0.0000000000 1.0000000
placeSouth 47.13214 97320.68 0.0004842972 0.9996136
我想重新显示没有拦截且没有包含短语“South”的地方的列表
我想尝试命名索引列,然后对其进行子集化,但没有成功。
[更新] 我添加了更多数据来理解为什么 George Sava 的回答也去掉了“北”
df <- data.frame(place = c("South","South","North","East","West"),
temperature = c(30,30,20,12,15),
outlookfine=c(TRUE,TRUE,FALSE,FALSE,TRUE)
)
glm.fit <- glm(outlookfine ~ .,df, family= binomial )
coef.glm <-coef(summary(glm.fit))
coef.glm[!grepl(pattern = ("South|Intercept"), rownames(coef.glm)),]
产出
Estimate Std. Error z value Pr(>|z|)
placeNorth 3.970197e-14 185277.1 2.142843e-19 1.0000000
placeWest 4.913214e+01 185277.2 2.651818e-04 0.9997884
如果您想将行名称保留为一列,那么您可以这样做:
library(tibble)
library(dplyr)
as.data.frame(coef.glm) %>%
rownames_to_column("x") %>%
filter(!grepl("Intercept|South", x))
输出
x Estimate Std. Error t value Pr(>|t|)
1 placeNorth -1.281975e-16 3.140185e-16 -0.4082483 0.7532483
要仅保留匹配(或不匹配)特定模式的行,您可以使用:
coef.glm[!grepl("South|Intercept", rownames(coef.glm)),]
请注意,当只有一行被选中时,这将成为一个向量。