3

I'd like to compare the coefficients from my own replication of a statistical model in the literature with the coefficients in the literature. I thougth I could just duplicate my model, and then plug in the coefficients from the literature and print both. But I'm getting this error:

Error in if (coef.var[i] == .global.coefficient.variables[j]) { : argument is of length zero

What is the easiest way to do this?

##fake data
var1<-rnorm(100)
var2<-rnorm(100)
df<-data.frame(var1, var2)
#My own model
model1<-lm(var1~var2, data=df)

#Duplicate my own model
model2<-model1
#Try to change the coefficients to match what appears in the literature
model2$coefficients<-c(5, 10)
#Try to report
library(stargazer)
stargazer(model1, model2,type="html")
spindoctor
  • 1,719
  • 1
  • 18
  • 42

1 Answers1

0

This works:

var1<-rnorm(100)
var2<-rnorm(100)
df<-data.frame(var1, var2)

model1<-lm(var1~var2, data=df)

model2<-model1

model2$coefficients[[1]] <- 5
model2$coefficients[[2]] <- 10


library(stargazer)
stargazer(model1, model2,type="html")
Ahorn
  • 3,686
  • 1
  • 10
  • 17
  • Hmm.. So is the object model2$coefficients a list itself? Or a vector? – spindoctor Jun 01 '20 at 19:00
  • It is a named vector. Is this what you wanted to do? – Ahorn Jun 01 '20 at 19:02
  • That's exactly what I wanted to do. I'm just curious to know why this didn't work. `model2$coefficients<-c(5, 10)` – spindoctor Jun 01 '20 at 19:08
  • @spindoctor; it seems as if it need the names; so you can use `attributes(model2$coefficients)=attributes(model1$coefficients)` .. in fact just using `model2$coefficients[1:2] = c(5,10)` does the trick – user20650 Jun 01 '20 at 19:31