R尽管没有足够的观测值,但继续在地图函数中进行t.test

时间:2019-03-15 09:05:46

标签: r t-test hypothesis-test

在我的示例数据中,我有3个数据框。每个df每个阈值都有2个变量(varA和varB)。有3个阈值(1、2、3):

df1 <- tibble(
var1A= rnorm(1:10) +1,
var1B= rnorm(1:10) +1,
var2A= rnorm(1:10) +2,
var2B= rnorm(1:10) +2,
var3A= rnorm(1:10) +3,
var3B= rnorm(1:10) +3)


df2 <- tibble(
var1A= rnorm(1:10) +1,
var1B= rnorm(1:10) +1,
var2A= rnorm(1:10) +2,
var2B= rnorm(1:10) +2,
var3A= rnorm(1:10) +3,
var3B= rnorm(1:10) +3)


df3 <- tibble(
var1A= rnorm(1:10) +1,
var1B= NA,
var2A= rnorm(1:10) +2,
var2B= rnorm(1:10) +2,
var3A= rnorm(1:10) +3,
var3B= rnorm(1:10) +3)

现在,我想对每个变量t.test(varA, varB)和每个阈值(1、2、3)执行t.test。 由于我有1个以上的df,因此我将所有df都放在了一个map函数中,并为所有df映射了t.test,并对所有阈值应用了t.test:

thresholds = c(1, 2, 3)

list_dfs = c('df1','df2','df3')

map(list_dfs,
function(df_name){
  x <- get(df_name)
  lapply(thresholds, function(i){
    t.test(x %>%
             pull(paste0("var",i,"A")), 
           x %>% 
             pull(paste0("var",i,"B")))
  }) %>% 
    map_df(broom::tidy) %>% 
    add_column(.before = 'estimate',
               df = df_name, 
               threshold = thresholds)
}) %>% 
do.call(rbind, .)

此代码会将所有结果映射到一个df中。但是问题在于var1B中的df3为空。整个列为NA

尽管对var1B的观测不足,但如何执行地图功能? 这是我想要的输出:

# A tibble: 9 x 12
  df    threshold estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method
  <chr>     <dbl>    <dbl>     <dbl>     <dbl>     <dbl>   <dbl>     <dbl>    <dbl>     <dbl> <chr> 
1 df1           1  -0.582      0.992     1.57    -1.43     0.170      16.6   -1.44      0.276 Welch~
2 df1           2   0.271      2.75      2.48     0.654    0.522      17.8   -0.601     1.14  Welch~
3 df1           3  -0.250      3.12      3.37    -0.544    0.593      17.7   -1.22      0.716 Welch~
4 df2           1  -0.169      0.747     0.916   -0.407    0.690      15.3   -1.05      0.714 Welch~
5 df2           2   0.0259     1.94      1.91     0.0702   0.945      17.9   -0.748     0.800 Welch~
6 df2           3   0.496      3.28      2.79     1.11     0.281      17.5   -0.444     1.44  Welch~
7 df3           1   NA         NA        NA       NA       NA         NA      NA        NA    NA   
8 df3           2  -0.274      1.99      2.26    -0.650    0.525      15.8   -1.17      0.622 Welch~
9 df3           3   0.407      3.34      2.93     0.920    0.371      16.6   -0.529     1.34  Welch~

由于df3 ist NA中阈值1的varB,输出中的第7行也是NA

2 个答案:

答案 0 :(得分:1)

我要做的是将stack-overflow组合成另一种格式-以便将“ A”部分放在一个 $result = $stmt->fetchAll(PDO::FETCH_OBJ); $result = $stmt->fetch(PDO::FETCH_OBJ); 和“ B”部分中-另一个:

data.frame

然后一切都变得简单了

data.frame

或者,您可以使用便捷的库dfs <- cbind(df1=df1, df2=df2, df3=df3) dfA <- dfs[,grep("A$", colnames(dfs))] dfB <- dfs[,grep("B$", colnames(dfs))]

doTtest <- function(x, y) {
  if(any(!is.na(x)) & any(!is.na(y)))
    broom::tidy(t.test(x,y))
  else
    rep(NA, 10)
}
res <- as.data.frame(t(mapply(doTtest, dfA, dfB)))

答案 1 :(得分:0)

另一种可能性是将t.test放在多个if函数中。 如果所有变量A和B的总和不为0,则执行t.test。其他粘贴NA

map(list_dfs,
function(df_name){
  x <- get(df_name)
  lapply(thresholds, function(i){
    if(sum(x%>%pull(paste0("var",i,"A")), na.rm = T) != 0){
      if(sum(x%>%pull(paste0("var",i,"B")), na.rm = T) != 0){
    t.test(x %>%
             pull(paste0("var",i,"A")), 
           x %>% 
             pull(paste0("var",i,"B")))
      } else NA
    } else NA

  }) %>% 
    map_df(broom::tidy)%>%
    add_column(.before = 'estimate',
               df = df_name, 
               threshold = thresholds)  
})      %>% bind_rows()
相关问题