在管道内收集 p 值 (dplyr)
Collecting p-values within pipe (dplyr)
你好吗?
所以,我有一个如下所示的数据集:
dirtax_trev indtax_trev lag2_majority pub_exp
<dbl> <dbl> <dbl> <dbl>
0.1542 0.5186 0 9754
0.1603 0.4935 0 9260
0.1511 0.5222 1 8926
0.2016 0.5501 0 9682
0.6555 0.2862 1 10447
我遇到了以下问题。我想沿着虚拟变量 (lag2_majority) 执行一系列 t.tests,收集此测试的 p 值,并使用管道将其归因于向量。
我想要 运行 这些 t 检验的所有变量都在下面选择,然后我为我的 t.test 变量(lag2_majority)省略了 NA 值,然后我尝试用这段代码总结一下:
test <- g %>%
select(dirtax_trev, indtax_trev, gdpc_ppp, pub_exp,
SOC_tot, balance, fdi, debt, polity2, chga_demo, b_gov, social_dem,
iaep_ufs, gini, pov4, informal, lab, al_ethnic, al_language, al_religion,
lag_left, lag2_left, majority, lag2_majority, left, system, b_system,
execrlc, allhouse, numvote, legelec, exelec, pr) %>%
na.omit(lag2_majority) %>%
summarise_all(funs(t.test(.[lag2_majority], .[lag2_majority == 1])$p.value))
但是,一旦我 运行 这样做,我得到的响应是:Error in summarise_impl(.data, dots): Evaluation error: data are essentially constant.
,这很令人困惑,因为虚拟变量的均值存在明显差异。当我将上面指示的代码的最后一行替换为:summarise_all(funs(t.test(.~lag2_majority)$p.value))
.
时出现相同的错误
或者,因为我只想做:t.test(dirtax_trev~lag2_majority, g)$p.value
,例如,我想我可以做一个循环,像这样:
for (i in vars){
t.test(i~lag2_majority, g)$p.value
}
,
其中 vars 是一个对象,其中包含在上述代码中选择的所有变量。但是我再次收到一条错误消息。具体来说,这个:Error in model.frame.default(formula = i ~ lag2_majority, data = g): comprimentos das variáveis diferem (encontradas em 'lag2_majority')
我做错了什么?
此致!
您的问题不可重现,请阅读 this 以了解如何提高其质量。
我的回答已被概括为可重现,因为我没有您的数据,因此无法直接调整您的代码。
使用 tidy 方法,我将为每个变量生成一个 p 值 的数据框。
library(tidyr)
library(dplyr)
library(purrr)
mtcars %>%
select_if(is.numeric) %>%
map(t.test) %>%
lapply(`[[`, "p.value") %>%
as_tibble %>%
gather(key, p.value)
# # A tibble: 11 x 2
# key p.value
# <chr> <dbl>
# 1 mpg 1.526151e-18
# 2 cyl 5.048147e-19
# 3 disp 9.189065e-12
# 4 hp 2.794134e-13
# 5 drat 1.377586e-27
# 6 wt 2.257406e-18
# 7 qsec 7.790282e-33
# 8 vs 2.776961e-05
# 9 am 6.632258e-05
# 10 gear 1.066949e-23
# 11 carb 4.590930e-11
更新
感谢您更新您的问题,请注意,您在之前的评论中包含的值可能来自您的原始数据集,并且在此处仍然无法重现。当我 运行 代码时,这是输出。
t.test(dirtax_trev ~ lag2_majority, g)$p.value
# [1] 0.5272474
请以任何人都能以与您相同的方式看待问题的方式来组织您的问题。
为了构建您运行通过t.test
的公式,我采用了稍微不同的方法。
library(magrittr)
library(dplyr)
library(purrr)
g <- tribble(
~dirtax_trev, ~indtax_trev, ~lag2_majority, ~pub_exp,
0.1542, 0.5186, 0, 9754,
0.1603, 0.4935, 0, 9260,
0.1511, 0.5222, 1, 8926,
0.2016, 0.5501, 0, 9682,
0.6555, 0.2862, 1, 10447
)
dummy <- "lag2_majority"
colnames(g) %>%
.[. != dummy] %>% # vector of variables to send through t.test
paste(., "~", dummy) %>% # build formula as character
map(as.formula) %>% # convert to formula class
map(t.test, data = g) %$% # run t.test for each, note the special operator
tibble(
data.name = unlist(lapply(., `[[`, "data.name")),
p.value = unlist(lapply(., `[[`, "p.value"))
)
# # A tibble: 3 x 2
# data.name p.value
# <chr> <dbl>
# 1 dirtax_trev by lag2_majority 0.5272474
# 2 indtax_trev by lag2_majority 0.5021217
# 3 pub_exp by lag2_majority 0.8998690
如果您希望从 data.name
中删除虚拟变量名称,您可以修改其在 tibble
中的赋值:
data.name = unlist(strsplit(unlist(lapply(., `[[`, "data.name")), paste(" by", dummy)))
N.B. I used the special %$%
from magrittr
to expose the names from the list of tests to build a data frame. I'm sure there are other ways that may be more elegant, however, I find this form quite easy to reason about.
你好吗?
所以,我有一个如下所示的数据集:
dirtax_trev indtax_trev lag2_majority pub_exp
<dbl> <dbl> <dbl> <dbl>
0.1542 0.5186 0 9754
0.1603 0.4935 0 9260
0.1511 0.5222 1 8926
0.2016 0.5501 0 9682
0.6555 0.2862 1 10447
我遇到了以下问题。我想沿着虚拟变量 (lag2_majority) 执行一系列 t.tests,收集此测试的 p 值,并使用管道将其归因于向量。
我想要 运行 这些 t 检验的所有变量都在下面选择,然后我为我的 t.test 变量(lag2_majority)省略了 NA 值,然后我尝试用这段代码总结一下:
test <- g %>%
select(dirtax_trev, indtax_trev, gdpc_ppp, pub_exp,
SOC_tot, balance, fdi, debt, polity2, chga_demo, b_gov, social_dem,
iaep_ufs, gini, pov4, informal, lab, al_ethnic, al_language, al_religion,
lag_left, lag2_left, majority, lag2_majority, left, system, b_system,
execrlc, allhouse, numvote, legelec, exelec, pr) %>%
na.omit(lag2_majority) %>%
summarise_all(funs(t.test(.[lag2_majority], .[lag2_majority == 1])$p.value))
但是,一旦我 运行 这样做,我得到的响应是:Error in summarise_impl(.data, dots): Evaluation error: data are essentially constant.
,这很令人困惑,因为虚拟变量的均值存在明显差异。当我将上面指示的代码的最后一行替换为:summarise_all(funs(t.test(.~lag2_majority)$p.value))
.
或者,因为我只想做:t.test(dirtax_trev~lag2_majority, g)$p.value
,例如,我想我可以做一个循环,像这样:
for (i in vars){
t.test(i~lag2_majority, g)$p.value
}
,
其中 vars 是一个对象,其中包含在上述代码中选择的所有变量。但是我再次收到一条错误消息。具体来说,这个:Error in model.frame.default(formula = i ~ lag2_majority, data = g): comprimentos das variáveis diferem (encontradas em 'lag2_majority')
我做错了什么?
此致!
您的问题不可重现,请阅读 this 以了解如何提高其质量。
我的回答已被概括为可重现,因为我没有您的数据,因此无法直接调整您的代码。
使用 tidy 方法,我将为每个变量生成一个 p 值 的数据框。
library(tidyr)
library(dplyr)
library(purrr)
mtcars %>%
select_if(is.numeric) %>%
map(t.test) %>%
lapply(`[[`, "p.value") %>%
as_tibble %>%
gather(key, p.value)
# # A tibble: 11 x 2
# key p.value
# <chr> <dbl>
# 1 mpg 1.526151e-18
# 2 cyl 5.048147e-19
# 3 disp 9.189065e-12
# 4 hp 2.794134e-13
# 5 drat 1.377586e-27
# 6 wt 2.257406e-18
# 7 qsec 7.790282e-33
# 8 vs 2.776961e-05
# 9 am 6.632258e-05
# 10 gear 1.066949e-23
# 11 carb 4.590930e-11
更新
感谢您更新您的问题,请注意,您在之前的评论中包含的值可能来自您的原始数据集,并且在此处仍然无法重现。当我 运行 代码时,这是输出。
t.test(dirtax_trev ~ lag2_majority, g)$p.value
# [1] 0.5272474
请以任何人都能以与您相同的方式看待问题的方式来组织您的问题。
为了构建您运行通过t.test
的公式,我采用了稍微不同的方法。
library(magrittr)
library(dplyr)
library(purrr)
g <- tribble(
~dirtax_trev, ~indtax_trev, ~lag2_majority, ~pub_exp,
0.1542, 0.5186, 0, 9754,
0.1603, 0.4935, 0, 9260,
0.1511, 0.5222, 1, 8926,
0.2016, 0.5501, 0, 9682,
0.6555, 0.2862, 1, 10447
)
dummy <- "lag2_majority"
colnames(g) %>%
.[. != dummy] %>% # vector of variables to send through t.test
paste(., "~", dummy) %>% # build formula as character
map(as.formula) %>% # convert to formula class
map(t.test, data = g) %$% # run t.test for each, note the special operator
tibble(
data.name = unlist(lapply(., `[[`, "data.name")),
p.value = unlist(lapply(., `[[`, "p.value"))
)
# # A tibble: 3 x 2
# data.name p.value
# <chr> <dbl>
# 1 dirtax_trev by lag2_majority 0.5272474
# 2 indtax_trev by lag2_majority 0.5021217
# 3 pub_exp by lag2_majority 0.8998690
如果您希望从 data.name
中删除虚拟变量名称,您可以修改其在 tibble
中的赋值:
data.name = unlist(strsplit(unlist(lapply(., `[[`, "data.name")), paste(" by", dummy)))
N.B. I used the special
%$%
frommagrittr
to expose the names from the list of tests to build a data frame. I'm sure there are other ways that may be more elegant, however, I find this form quite easy to reason about.