Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用加权t检验总结多列_R_Statistics_Tidyverse - Fatal编程技术网

R 使用加权t检验总结多列

R 使用加权t检验总结多列,r,statistics,tidyverse,R,Statistics,Tidyverse,我有以下数据,想计算加权p值。我复习了。但我的版本应该使用重量。我可以使用Code2来实现这一点。但是有30多列。如何有效地计算加权p值 代码1 # A tibble: 877 x 5 cat population farms farmland weight <chr> <dbl> <dbl> <dbl> <dbl> 1 Treated 9.89 8.00 12.3 1

我有以下数据,想计算加权p值。我复习了。但我的版本应该使用重量。我可以使用Code2来实现这一点。但是有30多列。如何有效地计算加权p值

代码1

# A tibble: 877 x 5
   cat     population farms farmland weight
   <chr>        <dbl> <dbl>    <dbl>  <dbl>
 1 Treated       9.89  8.00     12.3  1    
 2 Control      10.3   7.81     12.1  0.714
 3 Control      10.2   8.04     12.4  0.156
 4 Control      10.3   7.97     12.1  0.340
 5 Control      10.9   8.87     12.7  2.85 
 6 Control      10.4   8.35     12.5  0.934
 7 Control      10.5   8.58     12.9  0.193
 8 Control      10.6   8.57     12.6  0.276
 9 Control      10.2   8.54     12.5  0.344
10 Control      10.5   8.76     12.6  0.625
# … with 867 more rows

我们可以将
摘要
交叉使用

library(dplyr)
df %>%
   summarise(across(c(population:farmland),
   ~ weights::wtd.t.test(x = .[cat == 'Treated'],
                         y = .[cat == 'Control'], 
                         weight = weight[cat == 'Treated'],
                         weighty= weight[cat == 'Control'])$coefficients[3]))

或者使用
lapply/sapply

sapply(df[2:4], function(v)
         weights::wtd.t.test(x = v[df$cat == "Treated"],
                             y = v[df$cat == "Control"],
                             weight = df$weight[df$cat == "Treated"],
                   weighty = df$weight[df$cat == "Control"])$coefficients[3])
sapply(df[2:4], function(v)
         weights::wtd.t.test(x = v[df$cat == "Treated"],
                             y = v[df$cat == "Control"],
                             weight = df$weight[df$cat == "Treated"],
                   weighty = df$weight[df$cat == "Control"])$coefficients[3])