File size: 1,555 Bytes
204f58d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
library("lmtest")
library("sandwich")
set.seed(2020)

## Uses the output from the data build.

main <- function(mode = "test", drop_wave_7 = T){
  data <- read.csv("output/data.csv")
  
  if (drop_wave_7) data <- data[!(data$wave == 7), ]
  drop <- ifelse(drop_wave_7, "drop_7", "keep_7")
  
  if (mode == "test"){
    # Randomize data in each column independently for testing purposes.
    for (i in 3:ncol(data)){
      data[, i] <- sample(data[, i])
    }
  }
  
  # Baseline "Long" Model
  # https://www.r-econometrics.com/methods/hcrobusterrors/
  # https://stats.stackexchange.com/questions/117052/replicating-statas-robust-option-in-r (HC1 matches Stata robust option)
  model <- lm(gov_consumption ~ sd_gov + mean_gov +
                africa + laam + asiae +
                col_uka + col_espa + col_otha +
                federal + oecd +
                log_gdp_per_capita + trade_share + age_15_64 + age_65_plus, 
              data = data)
  out <- coeftest(model, vcov = vcovHC(model, type = "HC1")) 
  
  
  # Clean and produce table
  out <- rbind(out, c(length(model$residuals), "", "", ""))
  out <- rbind(out, c(summary(model)$r.squared, "", "", ""))
  rownames(out) <- c(rownames(out)[-c((nrow(out)-1):nrow(out))], "Obs.", "R-squared")
  write.table(out, file = sprintf("output/%s_%s.txt", mode, drop))
}

# Test analysis code
main(mode = "test", drop_wave_7 = T)
main(mode = "test", drop_wave_7 = F)

# Run on real data w/o wave 7
main(mode = "real", drop_wave_7 = T)

# Run on real data w/ wave 7
main(mode = "real", drop_wave_7 = F)