使用R创建系数的森林图以对子样本进行回归

如何解决使用R创建系数的森林图以对子样本进行回归

我有一个由100个组组成的国际象棋位置的数据集,每个组取50个位置之一(“ Position_number”)和两种颜色之一(“ stm_white”)。我想为每个Position_number子样本运行线性回归,其中stm_white是解释性变量,stm_perform是结果变量。然后,我想显示林图中每个回归的stm_white系数和相关的置信区间。这个想法是为了能够轻松地看到哪个Position_number子样本为stm_white给出了重要的系数,并且可以比较各个位置的系数。例如,该图具有50个y轴类别,每个类别均标有位置编号,x轴表示系数范围,并且该图将显示每个位置编号的水平置信度。

我被困的地方:

  1. 获取每次回归的置信区间界限
  2. 在一个图上绘制50个系数(具有置信区间)中的每一个。 (我认为这被称为森林图?)

这是我当前获取每次回归的系数列表的方式:

fits <- by(df,df[,"Position_number"],function(x) lm(stm_perform ~ stm_white,data = x))
# Combine coefficients from each model
do.call("rbind",lapply(fits,coef))

这是10个职位的示例(如果有更好的方法来显示可重复的数据,我们深表歉意):

>dput(droplevels(dfMWE[,c("Position_number","stm_white","stm_perform")]))
structure(list(Position_number = c(0,1,2,3,4,5,6,7,8,9,10,10),stm_white = c(0,1),stm_perform = c(0.224847134350316,-0.252000458803946,0.263005239459311,-0.337712202569111,0.525880930891169,-0.5,0.514387184165999,0.520136722035817,-0.471249436107731,-0.557311633762293,-0.382774969095054,-0.256365477992672,-0.592466230584332,0.420100239642119,0.35728693116738,-0.239203909010858,0.492804918290949,-0.377349804212738,0.498560888290847,0.650604627933873,0.244481117928803,0.225852022298169,0.448376452689039,0.305090287270497,0.275461757157464,0.0232950364735793,-0.117225030904946,0.103523492101814,0.098301745397805,0.435599509759579,-0.323024628921732,-0.790798102797238,0.326223812111678,-0.331305043692668,0.300230596737942,-0.340292005855252,0.196181480575316,-0.0606495585093978,0.789844179758131,-0.0862623926308338,-0.560150145231903,0.697345078589853,-0.425719796345476,0.65321716721887,-0.878090073942596,0.393712176214572,0.636076899687882,0.530184680003902,-0.567228844342952,0.767024918145021,-0.207303615824231,-0.332581578126777,-0.511510891217792,0.227871326531416,-0.0140876421179904,-0.891010911045765,-0.617225030904946,-0.335142021445235,-0.517262524432376,0.676301669492737,0.375998241382333,-0.0882899718631629,-0.154706189382,-0.108431333126633,0.204584592662721,0.475554538879339,0.0840205872617279,-0.403370826694226,-0.74253555894307,0.182570385474772,-0.484175014735265,-0.427127748605496,0.474119069108831,-0.0668284645696687,-0.0262098994728823,-0.255269593134965,-0.313699742316688,-0.485612815834001,0.302654921410147,0.60766106412682,0.384135895746244,0.564400490240421,0.702182602090521,0.518699777929559,-0.281243170101218,-0.283576305897061,0.349395372066127,-0.596629173305774,0.0849108889395813,-0.264122555898524,0.593855385236178,-0.418698521631085,0.269754586702576,-0.719919005947152,0.510072446927438,-0.0728722513945044,-0.0849108889395813,0.0650557537775339,0.063669188530584,-0.527315973006493,-0.716423694102939,-0.518699777929559,-0.361262250888275,0.431358608116332,0.104596852632671,0.198558626418023,0.753386077785615,0.418698521631085,-0.492804918290949,-0.636076899687882,-0.294218640287997,0.617225030904946,-0.333860575416878,-0.544494573083008,-0.738109032540419,-0.192575818328721,-0.442688366237707,0.455505426916992,0.13344335621046,0.116471711943561,0.836830966002895,-0.125024693001636,0.400603203290743,-0.363923100312118,-0.157741327529574,-0.326223812111678,-0.548774335859742,0.104058949158278,-0.618584122089031,-0.148779202375097,-0.543066492022212,-0.541637702714763,0.166337530816562,-0.431358608116332,-0.531618297828107,-0.135452994588696,0.444109038883147,-0.309993792719686,0.472684026993507,-0.672509643334985,-0.455505426916992,-0.0304828450187082,-0.668694956307332,0.213036720610531,-0.370611452782498,-0.100361684849949,-0.167940159469667,-0.256580594295053,0.41031649686005,0.544494573083008,-0.675040201040299,0.683816314193659,0.397841906825283,0.634743335052317,-0.598013765769344,-0.524445461120661,-0.613136820153143,0.12949974225673,-0.189904841395243,0.588289971863163,0.434184796930767,-0.703385003471829,0.505756208411145,0.445530625978324,-0.167137309739621,0.437015271896404,-0.550199353253537,-0.489927553072562,-0.791748837508184,0.264122555898524,-0.282408276808469,-0.574280203654524,0.167940159469667,-0.439849854768097,-0.604912902007957,0.239220254140668,-0.276612130560829,-0.25746444105693,-0.632070012100074,0.314483587504712,-0.226860086923233,-0.702182602090521,0.25746444105693,-0.174474012638818,0.0166045907672774,0.535915926945102,0.141635395826102,0.557311633762293,0.6961287704296,0.0444945730830079,-0.234005329233511,-0.86655664378954,0.22107824319756,0.148051654147426,0.543066492022212,-0.448376452689039,0.373300918333268)),row.names = c(NA,-220L
),groups = structure(list(Position_number = c(0,.rows = structure(list(1:20,21:40,41:60,61:80,81:100,101:120,121:140,141:160,161:180,181:200,201:220),ptype = integer(0),class = c("vctrs_list_of","vctrs_vctr","list"))),11L),class = c("tbl_df","tbl","data.frame"),.drop = TRUE),class = c("grouped_df","tbl_df","data.frame"))

解决方法

confint()可以为您提供模型的置信区间。
forestplot() R软件包中的forestplot可以使您成为森林图。

library(dplyr)
library(forestplot)

results <- lapply(unique(df$Position_number),function(pos) {
    fit = filter(df,Position_number == pos) %>% 
        lm(data = .,stm_perform ~ stm_white)
    stm_white_lm_index = 2 # the second term in lm() output is "stm_white"
    coefficient = coef(fit)[stm_white_lm_index]
    lb = confint(fit)[stm_white_lm_index,1] # lower bound confidence 
    ub = confint(fit)[stm_white_lm_index,2] # upper bound confidence
    output = data.frame(Position_number = pos,coefficient,lb,ub)
    return(output)
}) %>% bind_rows() # bind_rows() combines output from each model in the list

with(results,forestplot(Position_number,ub))

forestplot() output

森林图在左侧显示“ Position_number”标签,并以95%置信区间绘制“ stm_white”的回归系数。您可以进一步自定义图。有关详细信息,请参见Max Gordon的forestplot::forestplot()this introduction

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 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-