提问人:flâneur 提问时间:5/24/2023 最后编辑:flâneur 更新时间:5/26/2023 访问量:59
对数据帧 (R) 子集进行多个加权 t 检验的运行函数
Run function for multiple weighted t tests on subsets of dataframe (R)
问:
我正在对数据帧的不同子集运行一个函数进行多个加权 t 检验。我的功能主要如下:
library(weights)
group_list <- list(unique(df$group))
t_tests <- for (g in group_list){wtd.t.test(x=df[df$group == g,]$var2[df[df$group == g,]$var1=="A"],y=df[df$group == g,]$var2[df[df$group == g,]$var1=="B"],
weight=df[df$group == g,]$weight[df[df$group == g,]$var1=="A"],weighty=df[df$group == g,]$weight[df[df$group == g,]$var1=="B"],samedata=FALSE)}
其中 是感兴趣的结果变量。我想测试 = “A” 和 = “B” 均值之间差值的显著性,并针对变量的不同值对数据的每个子集执行此操作。var2
var1
var1
group
我使用了上面的代码,但错误是 我是否对函数的结构不正确?如何使这个加权 t 测试对数据帧的每个子集运行?Error in wtd.t.test(x = df[df$group == g, : object 'out' not found
更新:建议使用嵌套 tibbs 的新方法
我的新方法如下:
library(weights)
library(tidyverse)
df %>%
nest(-group) %>%
mutate(fit = map(data, ~ wtd.t.test(x=.%>%filter(var1 == "A")$var2,y=.%>% filter(var1 == "B")$var2,
weight=.%>% filter(var1 == "A")$weight,weighty=.%>% filter(var1 == "B")$weight,samedata=FALSE)),
results = map(fit, glance)) %>%
unnest(results)
新的错误消息是:
Error in `mutate()`:
ℹ In argument: `fit = map(...)`.
Caused by error in `map()`:
ℹ In index: 1.
Caused by error in `weight / mean(weight, na.rm = TRUE)`:
! non-numeric argument to binary operator
Backtrace:
1. ... %>% unnest(results)
10. purrr::map(...)
11. purrr:::map_("list", .x, .f, ..., .progress = .progress)
15. .f(.x[[i]], ...)
16. weights::wtd.t.test(...)
除了 Var1 之外,我的所有变量都是数字,它不用于计算,所以我不清楚为什么会出现此错误消息。任何建议将不胜感激。
如果我将代码重新格式化如下:
df %>%
nest(-country) %>%
mutate(fit = map(data, ~ wtd.t.test(x=filter(.,var1 == "A")$var2,y=filter(.,var1 == "B")$var2,
weight=filter(.,var1 == "A")$weight,weighty=filter(.,var1 == "B")$weight,samedata=FALSE)),
results = map(fit, glance)) %>%
unnest(results)
现在错误变为:
Error in `mutate()`:
ℹ In argument: `fit = map(...)`.
Caused by error in `map()`:
ℹ In index: 1.
Caused by error in `wtd.t.test()`:
! object 'out' not found
Backtrace:
1. ... %>% unnest(results)
10. purrr::map(...)
11. purrr:::map_("list", .x, .f, ..., .progress = .progress)
15. .f(.x[[i]], ...)
16. weights::wtd.t.test(...)
更新 2
下面是用可重现的示例更新的新代码:
library(weights)
library(tidyverse)
mtcars %>%
nest(-cyl) %>%
mutate(fit = map(data, ~ wtd.t.test(x=.%>%filter(gear == 3)$disp,y=.%>% filter(gear = 4)$disp,
weight=.%>% filter(gear == 3)$wt,weighty=.%>% filter(gear == 4)$wt,samedata=FALSE)),
results = map(fit, glance)) %>%
unnest(results)
并重新格式化:
mtcars %>%
nest(-cyl) %>%
mutate(fit = map(data, ~ wtd.t.test(x=filter(.,gear == 3)$disp,y=filter(.,gear == 4)$disp,
weight=filter(.,gear == 3)$weight,weighty=filter(.,gear == 4)$weight,samedata=FALSE)),
results = map(fit, glance)) %>%
unnest(results)
答:
0赞
flâneur
5/26/2023
#1
对于那些感兴趣的人,解决方案(使用数据集作为示例数据)如下:mtcars
library(tidyverse)
library(weights)
df_list <- split(mtcars, mtcars$cyl)
multiple_wt_ttest <- function(df) {ttest = wtd.t.test(x=subset(df, gear == 3)$disp,y=subset(df, gear == 4)$disp,
weight=subset(df, gear == 3)$wt,weighty=subset(df, gear == 4)$wt,samedata=FALSE)
out <<- ttest[2]}
data_store <- do.call(rbind, sapply(df_list,multiple_wt_ttest))
这将生成一个数据帧,其中包含每个级别的每个数据子集的 t 检验检验统计量。cyl
上一个:遍历多个数据集没有结果
评论