首页 > 解决方案 > R Purrr - coeftest

问题描述

library(lmtest)
library(tidyverse)
library(texreg)

structure(list(tg = structure(c(1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 
1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 
2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L), .Label = c("0", "1"), class = "factor"), 
    hosptg = structure(c(1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 
    2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 
    1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L), .Label = c("1", 
    "2", "3"), class = "factor"), quarter.adm = structure(c(8L, 
    12L, 12L, 10L, 11L, 10L, 4L, 12L, 12L, 6L, 10L, 4L, 9L, 9L, 
    4L, 5L, 5L, 9L, 8L, 8L, 5L, 9L, 9L, 4L, 8L, 8L, 9L, 4L, 3L, 
    10L, 5L, 9L, 11L, 5L, 10L, 9L, 8L, 1L, 11L, 9L, 11L, 6L, 
    11L, 12L, 8L, 7L, 1L, 3L, 10L, 1L), .Label = c("2011Q1", 
    "2011Q2", "2011Q3", "2011Q4", "2012Q1", "2012Q2", "2012Q3", 
    "2012Q4", "2013Q1", "2013Q2", "2013Q3", "2013Q4"), class = "factor"), 
    year = structure(c(2L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 2L, 
    3L, 1L, 3L, 3L, 1L, 2L, 2L, 3L, 2L, 2L, 2L, 3L, 3L, 1L, 2L, 
    2L, 3L, 1L, 1L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 2L, 1L, 3L, 3L, 
    3L, 2L, 3L, 3L, 2L, 2L, 1L, 1L, 3L, 1L), .Label = c("2011", 
    "2012", "2013"), class = "factor"), g.mdc = structure(c(6L, 
    6L, 5L, 5L, 9L, 8L, 16L, 14L, 9L, 8L, 9L, 3L, 8L, 9L, 1L, 
    1L, 7L, 9L, 5L, 5L, 2L, 3L, 5L, 4L, 3L, 15L, 25L, 4L, 8L, 
    17L, 8L, 14L, 13L, 8L, 13L, 1L, 4L, 8L, 1L, 11L, 17L, 14L, 
    7L, 5L, 19L, 8L, 13L, 14L, 17L, 8L), .Label = c("01", "02", 
    "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", 
    "13", "14", "15", "16", "17", "18A", "18B", "19", "20", "21A", 
    "21B", "22", "23"), class = "factor"), age = c(77, 34, 92, 
    80, 80, 54.2, 71, 34, 73, 69, 58, 30.5, 25, 58.3333333333333, 
    74, 74, 52, 65, 67, 54, 62, 63, 53, 57, 61.6, 0, 44, 89, 
    23, 59, 47, 26, 59.5, 69, 36, 1, 82, 46.6666666666667, 88, 
    77, 79, 35.5, 52, 65, 54, 74.8965517241379, 26, 27, 85, 75
    ), m.mortf = c(0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0), n = c(1L, 
    1L, 2L, 1L, 1L, 5L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 3L, 3L, 1L, 
    1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 5L, 6L, 1L, 1L, 1L, 1L, 2L, 
    1L, 2L, 1L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 29L, 
    1L, 1L, 1L, 1L)), row.names = c(66955L, 186063L, 102213L, 
84222L, 180071L, 85825L, 36157L, 188083L, 104028L, 145825L, 173253L, 
129379L, 77246L, 77310L, 27709L, 37303L, 36458L, 77466L, 66049L, 
158292L, 37632L, 163463L, 75336L, 29069L, 64354L, 69645L, 81063L, 
35344L, 124809L, 174579L, 139201L, 167354L, 180826L, 36968L, 
89644L, 73224L, 70970L, 5284L, 176275L, 77867L, 181299L, 51621L, 
98109L, 185560L, 70122L, 58881L, 6489L, 126620L, 174674L, 8534L
), class = "data.frame")

第一次回归

r.dnd1.1.1   <- lm(m.mortf ~ year*tg, data=subset(data,  
                                                    hosptg != '3' ), 
                   na.action = na.omit, weights = n) 

第二次回归(21 次)

r.dnd1.1.11   <- lm(m.mortf ~ quarter.adm*tg, data=subset(data,  year %in% c('2011', '2012', '2013') & 
                                                           hosptg != '3' ), 
                   na.action = na.omit, weights = n) 

我需要稳健的估计器,并希望用稳健估计的替换值来丰富我的结果表(库(texreg)),即我还需要命名变量

coeftest(r.dnd1.1.1, vcov=vcovHC) #-- either p-values or S.E. -> works fine

我有几个模型,我想从中提取稳健的估计量。提取我当前的 21 个模型(在这个例子中只有 2 个)

r.mod <- as.list(ls(pattern="r.dnd1.1."))

创建一个包含所有结果的数据框,然后可以在以后重用 texreg 替换原始估计器

test  <- map(r.mod, ~coeftest(.x , vcov=vcovHC))

但我得到了错误:$ operator is invalid for atomic vectors 我的错误是什么?预先感谢您的支持。

标签: rdictionarypurrr

解决方案


您可以使用map(), 但.x被评估为字符串。有几种方法可以告诉 R 将字符串评估为拟合对象 - 这是使用andlm的一种方法:eval()parse()

library(purrr)
purrr::map(r.mod, 
          ~coeftest(x = eval(parse(text=.x)), 
                    vcov. = vcov(eval(parse(text=.x)))))

推荐阅读