如何在R中的列表中插入pvalue低于5%的回归器变量名
我试图在R中构建一个列表,其中包含所有的回归器名称,这些名称的pvalue低于5%的阈值。 例如: 第一次回归如何在R中的列表中插入pvalue低于5%的回归器变量名,r,list,R,List,我试图在R中构建一个列表,其中包含所有的回归器名称,这些名称的pvalue低于5%的阈值。 例如: 第一次回归 #gender (male - female) regr1 <- lm(salary ~ female, data = test) summary(regr1) #性别(男-女) 第1 | t |条) (截距)0.855618 0.001888 453.24您可以使用broom::tidy然后操作表,如下所示: library(tidyverse) tab <- lm(
#gender (male - female)
regr1 <- lm(salary ~ female, data = test)
summary(regr1)
#性别(男-女)
第1 | t |条)
(截距)0.855618 0.001888 453.24您可以使用broom::tidy
然后操作表,如下所示:
library(tidyverse)
tab <- lm(data = mtcars, mpg ~ cyl + disp + hp) %>% summary() %>% broom::tidy()
tab
# A tibble: 4 x 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 34.2 2.59 13.2 1.54e-13
2 cyl -1.23 0.797 -1.54 1.35e- 1
3 disp -0.0188 0.0104 -1.81 8.09e- 2
4 hp -0.0147 0.0147 -1.00 3.25e- 1
现在,您可以使用回归器名称:
tab %>% filter(p.value < 0.05) %>% select(term) %>% as.character()
[1] "(Intercept)"
tab%%>%filter(p.value<0.05)%%>%select(term)%%>%as.character()
[1] (拦截)
在baseR
中,可以执行以下操作:
lr1 <- lm(Sepal.Length ~ ., data = iris)
coef_table <- coef(summary(tab))
row.names(coef_table)[coef_table[, "Pr(>|t|)"] < 0.001]
# "(Intercept)" "Sepal.Width" "Petal.Length"
lr1
varnames <- c("female", "Bachelor", "HighSchool", "None")
library(tidyverse)
tab <- lm(data = mtcars, mpg ~ cyl + disp + hp) %>% summary() %>% broom::tidy()
tab
# A tibble: 4 x 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 34.2 2.59 13.2 1.54e-13
2 cyl -1.23 0.797 -1.54 1.35e- 1
3 disp -0.0188 0.0104 -1.81 8.09e- 2
4 hp -0.0147 0.0147 -1.00 3.25e- 1
tab %>% filter(p.value < 0.05)
# A tibble: 1 x 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 34.2 2.59 13.2 1.54e-13
tab %>% filter(p.value < 0.05) %>% select(term) %>% as.character()
[1] "(Intercept)"
lr1 <- lm(Sepal.Length ~ ., data = iris)
coef_table <- coef(summary(tab))
row.names(coef_table)[coef_table[, "Pr(>|t|)"] < 0.001]
# "(Intercept)" "Sepal.Width" "Petal.Length"