对两个数据框列表执行多个两个样本 t 检验

Performing multiple two sample t-tests on two lists of data frames

我有两个列表,每个列表有四个数据框。第一个列表(“loc_list_OBS”)中的数据框只有“Year”和“Mean_Precip”两列,而第二个列表(“loc_list_future”)中的数据框有 33 “年”列,然后是 32 种不同模型的平均降水值。

因此 loc_list_OBS 中的数据框看起来像这样,但数据一直持续到 2005 年:

Year     Mean_Precip
1950    799.1309
1951    748.0239
1952    619.7572
1953    799.9263
1954    680.9194
1955    766.2304
1956    599.5365
1957    717.8912
1958    739.4901
1959    707.1130
...     ....
2005    ....

loc_list_future 中的数据框看起来像这样,但总共有 32 个模型列,数据转到 2059 年:

Year   Model 1      Model 2      Model 3    ...... Model 32
2020    714.1101    686.5888    1048.4274
2021    1018.0095    766.9161     514.2700
2022    756.7066    902.2542     906.2877
2023    906.9675    919.5234     647.6630
2024    767.4008    861.1275     700.2612
2025    876.1538    738.8370     664.3342
2026    781.5092    801.2387     743.8965
2027    876.3522    819.4323     675.3022
2028    626.9468    927.0774     696.1884
2029    752.4084    824.7682     835.1566
....    .....       .....         .....
2059    .....       .....         .....

每个数据框代表一个地理位置,两个列表具有相同的四个位置,但一个列表用于观测值,另一个用于预测未来值。

我想 运行 两个样本 t 检验,将观察值与每个模型在每个位置的预测未来值进行比较。换句话说,我想比较每个列表中的第一个数据框,然后是每个列表中的第二个数据框,第三个和第四个数据框也是如此。

这是我用过的代码:

t_stat = NULL
mapply(FUN = function(f, o) {
 t_stat <- t.test(o$Mean_Precip, f, alternative = "two.sided")  
}, f = loc_list_ttest, o = loc_list_OBS, SIMPLIFY = FALSE)
t_stat

这段代码只给了我四个 t 检验输出,它们将观察到的数据中的“Mean_Precip”列与未来数据中所有模型的组合进行比较。但是,我需要对每个位置的每个模型进行 t 检验。谁能知道怎么做?

您可以通过这样的方法解决问题。我知道您想将每个数据帧与其他数据帧进行比较,并为第二个数据帧中的每个变量获得 t-test 。一种方法是创建一个函数来遍历第二个数据框中的变量,然后将结果保存在列表中。您将有四个列表,每个列表中都有 t-test。我根据您分享的内容创建了虚拟数据:

#Data
df <- structure(list(Year = c(1950L, 1951L, 1952L, 1953L, 1954L, 1955L, 
1956L, 1957L, 1958L, 1959L, 2005L), Mean_Precip = c(799.1309, 
748.0239, 619.7572, 799.9263, 680.9194, 766.2304, 599.5365, 717.8912, 
739.4901, 707.113, 707.113)), class = "data.frame", row.names = c(NA, 
-11L))
#Data2
df1 <- structure(list(Year = c(2020L, 2021L, 2022L, 2023L, 2024L, 2025L, 
2026L, 2027L, 2028L, 2029L, 2059L), Model.1 = c(714.1101, 1018.0095, 
756.7066, 906.9675, 767.4008, 876.1538, 781.5092, 876.3522, 626.9468, 
752.4084, 752.4084), Model.2 = c(686.5888, 766.9161, 902.2542, 
919.5234, 861.1275, 738.837, 801.2387, 819.4323, 927.0774, 824.7682, 
824.7682), Model.3 = c(1048.4274, 514.27, 906.2877, 647.663, 
700.2612, 664.3342, 743.8965, 675.3022, 696.1884, 835.1566, 835.1566
)), class = "data.frame", row.names = c(NA, -11L))

现在,我们将创建列表(您必须拥有它们):

#Lists
List1 <- list(df1=df,df2=df,df3=df,df4=df)
List2 <- list(df1=df1,df2=df1,df3=df1,df4=df1)

函数如下:

#Function
myfun <- function(x,y)
{
  l <- x$Mean_Precip
  #Empty list
  List <- list()
  #Now loop
  for(i in 2:dim(y)[2])
  {
    #Label
    val <- names(y[,i,drop=F])
    r <- y[,i]
    #Test
    test <- t.test(l, r, alternative = "two.sided") 
    #Save
    List[[i-1]] <- test
    names(List)[i-1] <- val
  }
  return(List)
}

最后,我们申请:

#Apply
t.stat <- mapply(FUN = myfun,x=List1,y=List2,SIMPLIFY = FALSE)

输出是一个列表列表,您可以按以下方式探索每个元素:

t.stat[[1]]

在哪里可以找到比较第一个数据帧与第二个数据帧的所有变量的结果:

输出:

$Model.1

    Welch Two Sample t-test

data:  l and r
t = -2.2645, df = 16.448, p-value = 0.03738
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -165.949710   -5.657818
sample estimates:
mean of x mean of y 
 716.8302  802.6339 


$Model.2

    Welch Two Sample t-test

data:  l and r
t = -3.5901, df = 19.56, p-value = 0.001881
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -170.75516  -45.13574
sample estimates:
mean of x mean of y 
 716.8302  824.7756 


$Model.3

    Welch Two Sample t-test

data:  l and r
t = -0.72149, df = 13.829, p-value = 0.4826
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -138.01368   68.59334
sample estimates:
mean of x mean of y 
 716.8302  751.5403 

这是一种做你想做的事情的方法,尽管如果预测是基于观察的,p-values 的有效性是值得怀疑的,因为两个“样本”不是独立的。

results <- lapply(1:4, function(y) lapply(loc_list_future[[y]][, -1],
      function(x) t.test(loc_list_OBS[[y]], x)))
names(results) <- c("Region 1", "Region 2", "Region 3", "Region 4")

results 将是一个包含四个列表的列表,每个区域一个。在每个区域列表中将是每个模型的列表。 results[[1]] 为您提供区域 1 中所有模型的结果,results[[1]][[1]] 为您提供区域 1 模型 1 的结果。