Reputation: 185
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")
1st regression
r.dnd1.1.1 <- lm(m.mortf ~ year*tg, data=subset(data,
hosptg != '3' ),
na.action = na.omit, weights = n)
2nd regression (of 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)
I require robust estimators and want to enrich my result tables (library(texreg)) with the replacement values of the robust estimates i.e. I will require the naming of the variables as well
coeftest(r.dnd1.1.1, vcov=vcovHC) #-- either p-values or S.E. -> works fine
I have several models for which I would like to extract the robust estimators. extract my current 21 models (in this example only 2)
r.mod <- as.list(ls(pattern="r.dnd1.1."))
create a data frame with all results that can then be reused later for texreg replacement of original estimators
test <- map(r.mod, ~coeftest(.x , vcov=vcovHC))
But I get the error: $ operator is invalid for atomic vectors
What is my mistake?
Thanks in advance for your support.
Upvotes: 0
Views: 162
Reputation: 21274
You can use map()
, but .x
is getting evaluated as a string. There are a few ways to tell R to evaluate the string as the fitted lm
object - here's one way using eval()
and parse()
:
library(purrr)
purrr::map(r.mod,
~coeftest(x = eval(parse(text=.x)),
vcov. = vcov(eval(parse(text=.x)))))
Upvotes: 1