在两个数据帧列表上执行多个两个样本t检验

问题描述

我有两个带有四个数据帧的列表。第一个列表(“ loc_list_OBS”)中的数据帧只有两列“ Year”和“ Mean_Precip”,而第二个列表(“ loc_list_future”)中的数据帧具有33列“ Year”,然后分别表示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个Model列,数据流到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检验。一种方法是创建一个在第二个数据帧中的变量之间循环的函数,然后将结果保存在列表中。您将有四个列表,每个列表中都包含所有t检验。我已经根据您共享的内容创建了虚拟数据:

#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
)),-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值的有效性值得怀疑。

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的结果。