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

如何解决在具有许多列的两个数据帧列表上执行多个两个样本t检验

我有两个带有四个数据帧的列表。这两个列表中的数据框(“ loc_list_future”和“ loc_list_2019”)都有33列:“年”,然后是32种不同气候模型的平均降水值。

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   

loc_list_2019中的数据帧的年份范围为2006-2019,但其他方面看起来相同。

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

我想进行两个样本的t检验,以比较2006-19年度的值与每个地点每个模型的未来值。

我还有另一个列表(loc_list_OBS),它的数据帧只有两列“ Year”和“ Mean_Precip”(这是观察到的数据不是基于模型的,这就是为什么平均值只有一列的原因)。我有代码(见下文),将针对将来的数据(loc_list_future)针对观察到的数据(loc_list_OBS)运行两次样本t检验,但是我不确定如何更改此代码以对两个列表运行t检验每个都有32个模型。

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) 

解决方法

我建议采用下一种方法。我已经创建了类似于您所拥有的虚拟数据。这里的代码:

#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)),-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,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,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 

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-