Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Loops_Statistics_T Test - Fatal编程技术网

R 对具有多个列的两个数据帧列表执行多个双样本t检验

R 对具有多个列的两个数据帧列表执行多个双样本t检验,r,loops,statistics,t-test,R,Loops,Statistics,T Test,我有两个列表,每个列表有四个数据帧。两个列表(“loc_列表_未来”和“loc_列表_2019”)中的数据框有33列:“年”,然后是32个不同气候模型的平均降水量值 loc_list_future中的数据框如下所示,但共有32个模型列,数据将进入2059年: Year Model 1 Model 2 Model 3 ...Model 32 2020 714.1101 686.5888 1048.4274 2021 1018.0095

我有两个列表,每个列表有四个数据帧。两个列表(“loc_列表_未来”和“loc_列表_2019”)中的数据框有33列:“年”,然后是32个不同气候模型的平均降水量值

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   
2019年loc_列表中的数据帧的年份范围为2006-2019年,但在其他方面看起来相同

每个数据框代表一个地理位置,两个列表具有相同的四个位置,但一个列表用于2006-2019年的值,另一个用于未来的值

我想运行两个样本t检验,将2006-19年的值与每个位置每个模型的未来值进行比较

我有另一个列表(loc_list_OBS),其中数据帧只有两列“Year”和“Mean_Precip”(这是不基于模型的观测数据,这就是为什么Mean Precip只有一列)。我有代码(见下文)将针对未来数据(loc_list_future)对观测数据(loc_list_OBS)运行两个样本t测试,但我不确定如何更改此代码以对两个列表(每个列表有32个模型)运行t-tests

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

t.stat <- mapply(FUN = myfun,x=loc_list_OBS,y=loc_list_future, SIMPLIFY = FALSE) 

myfun我建议下一种方法。我创建了与您所拥有的类似的虚拟数据。下面是代码:

#Data before
dfb <- structure(list(Year = 2010:2019, Model.1 = c(614.1101, 918.0095, 
656.7066, 806.9675, 667.4008, 776.1538, 681.5092, 776.3522, 526.9468, 
652.4084), Model.2 = c(586.5888, 666.9161, 802.2542, 819.5234, 
761.1275, 638.837, 701.2387, 719.4323, 827.0774, 724.7682), Model.3 = c(948.4274, 
414.27, 806.2877, 547.663, 600.2612, 564.3342, 643.8965, 575.3022, 
596.1884, 735.1566)), class = "data.frame", row.names = c(NA, 
-10L))
#Data after
dfa <- structure(list(Year = 2020:2029, Model.1 = c(714.1101, 1018.0095, 
756.7066, 906.9675, 767.4008, 876.1538, 781.5092, 876.3522, 626.9468, 
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), Model.3 = c(1048.4274, 
514.27, 906.2877, 647.663, 700.2612, 664.3342, 743.8965, 675.3022, 
696.1884, 835.1566)), class = "data.frame", row.names = c(NA, 
-10L))
#Data for lists
L.before <- list(df1=dfb,df2=dfb,df3=dfb,df4=dfb)
L.after <- list(df1=dfa,df2=dfa,df3=dfa,df4=dfa)

让我知道这是否对您有效!

是的,这非常有效!再次感谢您,鸭子!我非常感谢您的帮助。
#Function
myfun <- function(x,y)
{
  #Create empty list
  List <- list()
  #Loop
  for(i in 2:dim(x)[2])
  {
    name <- names(x[,i,drop=F])
    before <- x[,i]
    after <- y[,i]
    #Test
    test <- t.test(before, after, alternative = "two.sided") 
    #Save
    List[[i-1]] <- test
    names(List)[i-1] <- name
  }
  return(List)
}
#Apply
t.stat <- mapply(FUN = myfun,x=L.before,y=L.after, SIMPLIFY = FALSE)
t.stat[[1]]

$Model.1

    Welch Two Sample t-test

data:  before and after
t = -1.9966, df = 18, p-value = 0.06122
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -205.224021    5.224021
sample estimates:
mean of x mean of y 
 707.6565  807.6565 


$Model.2

    Welch Two Sample t-test

data:  before and after
t = -2.8054, df = 18, p-value = 0.0117
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -174.88934  -25.11066
sample estimates:
mean of x mean of y 
 724.7764  824.7764 


$Model.3

    Welch Two Sample t-test

data:  before and after
t = -1.4829, df = 18, p-value = 0.1554
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -241.67613   41.67613
sample estimates:
mean of x mean of y 
 643.1787  743.1787