我有两个列表,每个列表有四个数据帧。第一列表中的数据帧("loc_list_OBS"(只有两列;年份";以及";Mean_ Preip";而第二列表("loc_list_future"(中的数据帧具有33列"loc_list";年份";然后是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_ Preip";列与未来数据中所有模型的组合。然而,我需要对每个位置的每个模型进行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
)), 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值的有效性是可疑的,因为两个";样品";不是独立的。
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提供结果。