Skip to content

Commit

Permalink
skipping the Koch et al replication on CRAN
Browse files Browse the repository at this point in the history
  • Loading branch information
moritzpschwarz committed Mar 19, 2024
1 parent 92283a8 commit f0a2d33
Showing 1 changed file with 150 additions and 148 deletions.
298 changes: 150 additions & 148 deletions tests/testthat/test-7-koch-et-al.R
Original file line number Diff line number Diff line change
@@ -1,148 +1,152 @@


# Prepare Data
set.seed(1230)
#data <- read.csv("CO2DriversEU_dataset.csv")
data("EU_emissions_road")
data <- EU_emissions_road
data$lgdp_sq <- data$lgdp^2

data$transport.emissions_pc <- data$transport.emissions/data$pop
data$ltransport.emissions_pc <- log(data$transport.emissions_pc)

data$L1.ltransport.emissions_pc <- NA
# For each country, shift the values of 'ltransport.emissions_pc' by one position
for (i in unique(data$country)) {
# Extract the 'ltransport.emissions_pc' values for the current country
current_country_values <- data$ltransport.emissions_pc[data$country == i]

# Shift the values by one position and insert an NA value at the beginning
shifted_values <- c(NA, current_country_values[-length(current_country_values)])

# Assign the shifted values to the corresponding rows in 'L1.ltransport.emissions_pc'
data$L1.ltransport.emissions_pc[data$country == i] <- shifted_values
}

# data %>%
# dplyr::group_by(country) %>%
# dplyr::mutate(test = dplyr::lag(ltransport.emissions_pc)) %>%
# dplyr::ungroup() %>%
# dplyr::mutate(istrue = test == L1.ltransport.emissions_pc) %>%
# dplyr::distinct(istrue)


# Group specification
EU15 <- c("Austria", "Belgium", "Germany", "Denmark", "Spain", "Finland",
"France", "United Kingdom", "Ireland", "Italy", "Luxembourg",
"Netherlands", "Greece", "Portugal", "Sweden")
# EU16 <- c("Croatia", "Bulgaria", "Cyprus","Czech Republic", "Estonia",
# "Hungary", "Lithuania", "Latvia", "Malta", "Poland", "Romania",
# "Slovak Republic", "Slovenia", "Switzerland", "Iceland",
# "Norway")
# EU31 <- c(EU15, EU16)



###### Analysis:

# Prepare sample and data
sample <- EU15
dat <- data[data$country %in% sample & data$year>=1995,]



# Break analysis:
is1 <- isatpanel(
data = dat,
formula = ltransport.emissions_pc ~ lgdp + lgdp_sq + lpop,
index = c("country", "year"),
effect = "twoways",
iis = T,
fesis = T,
t.pval=.05,
print.searchinfo = FALSE
)


# Break analysis:
is2 <- isatpanel(
data = dat,
formula = ltransport.emissions_pc ~ lgdp + lgdp_sq + lpop,
index = c("country", "year"),
effect = "twoways",
iis = T,
fesis = T,
t.pval=.01,
print.searchinfo = FALSE
)



# Break analysis:
is3 <- isatpanel(
data = dat,
formula = ltransport.emissions_pc ~ lgdp + lgdp_sq + lpop,
index = c("country", "year"),
effect = "twoways",
iis = T,
fesis = T,
t.pval=.001,
print.searchinfo = FALSE
)

get_countries <- function(x){
df <- x$estimateddata
indicators <- x$isatpanel.result$aux$mX
indicators <- indicators[,!colnames(indicators) %in% names(df)]
df <- cbind(df,indicators)
identify_indicator_timings(df, uis_breaks = NULL)
}

is1_coef <- coef(is1$isatpanel.result)[get_countries(is1)$fesis$name]
is2_coef <- coef(is2$isatpanel.result)[get_countries(is2)$fesis$name]
is3_coef <- coef(is3$isatpanel.result)[get_countries(is3)$fesis$name]

is1_coef <- is1_coef[order(names(is1_coef))]
is2_coef <- is2_coef[order(names(is2_coef))]
is3_coef <- is3_coef[order(names(is3_coef))]


is1_tib <- data.frame(name = c("fesisFinland.2000",
"fesisGermany.2002",
"fesisIreland.2011",
"fesisIreland.2015",
"fesisLuxembourg.2007",
"fesisSweden.2001"),
coef = c(-0.103,
-0.105,
-0.087,
-0.148,
-0.136,
-0.095))


is2_tib <- data.frame(name = c("fesisFinland.2000",
"fesisGermany.2002",
"fesisIreland.2015",
"fesisSweden.2001"),
coef = c(-0.123,
-0.131,
-0.192,
-0.103))


is3_tib <- data.frame(name = c("fesisFinland.2000",
"fesisGermany.2002",
"fesisIreland.2011",
"fesisLuxembourg.2015",
"fesisSweden.2001"),
coef = c(-0.128,
-0.108,
-0.127,
-0.214,
-0.110))

test_that("Equal Breaks as in Koch et al are identified",{
skip_on_cran()

# Prepare Data
set.seed(1230)
#data <- read.csv("CO2DriversEU_dataset.csv")
data("EU_emissions_road")
data <- EU_emissions_road
data$lgdp_sq <- data$lgdp^2

data$transport.emissions_pc <- data$transport.emissions/data$pop
data$ltransport.emissions_pc <- log(data$transport.emissions_pc)

data$L1.ltransport.emissions_pc <- NA
# For each country, shift the values of 'ltransport.emissions_pc' by one position
for (i in unique(data$country)) {
# Extract the 'ltransport.emissions_pc' values for the current country
current_country_values <- data$ltransport.emissions_pc[data$country == i]

# Shift the values by one position and insert an NA value at the beginning
shifted_values <- c(NA, current_country_values[-length(current_country_values)])

# Assign the shifted values to the corresponding rows in 'L1.ltransport.emissions_pc'
data$L1.ltransport.emissions_pc[data$country == i] <- shifted_values
}

# data %>%
# dplyr::group_by(country) %>%
# dplyr::mutate(test = dplyr::lag(ltransport.emissions_pc)) %>%
# dplyr::ungroup() %>%
# dplyr::mutate(istrue = test == L1.ltransport.emissions_pc) %>%
# dplyr::distinct(istrue)


# Group specification
EU15 <- c("Austria", "Belgium", "Germany", "Denmark", "Spain", "Finland",
"France", "United Kingdom", "Ireland", "Italy", "Luxembourg",
"Netherlands", "Greece", "Portugal", "Sweden")
# EU16 <- c("Croatia", "Bulgaria", "Cyprus","Czech Republic", "Estonia",
# "Hungary", "Lithuania", "Latvia", "Malta", "Poland", "Romania",
# "Slovak Republic", "Slovenia", "Switzerland", "Iceland",
# "Norway")
# EU31 <- c(EU15, EU16)



###### Analysis:

# Prepare sample and data
sample <- EU15
dat <- data[data$country %in% sample & data$year>=1995,]



# Break analysis:
is1 <- isatpanel(
data = dat,
formula = ltransport.emissions_pc ~ lgdp + lgdp_sq + lpop,
index = c("country", "year"),
effect = "twoways",
iis = T,
fesis = T,
t.pval=.05,
print.searchinfo = FALSE
)


# Break analysis:
is2 <- isatpanel(
data = dat,
formula = ltransport.emissions_pc ~ lgdp + lgdp_sq + lpop,
index = c("country", "year"),
effect = "twoways",
iis = T,
fesis = T,
t.pval=.01,
print.searchinfo = FALSE
)



# Break analysis:
is3 <- isatpanel(
data = dat,
formula = ltransport.emissions_pc ~ lgdp + lgdp_sq + lpop,
index = c("country", "year"),
effect = "twoways",
iis = T,
fesis = T,
t.pval=.001,
print.searchinfo = FALSE
)

get_countries <- function(x){
df <- x$estimateddata
indicators <- x$isatpanel.result$aux$mX
indicators <- indicators[,!colnames(indicators) %in% names(df)]
df <- cbind(df,indicators)
identify_indicator_timings(df, uis_breaks = NULL)
}

is1_coef <- coef(is1$isatpanel.result)[get_countries(is1)$fesis$name]
is2_coef <- coef(is2$isatpanel.result)[get_countries(is2)$fesis$name]
is3_coef <- coef(is3$isatpanel.result)[get_countries(is3)$fesis$name]

is1_coef <- is1_coef[order(names(is1_coef))]
is2_coef <- is2_coef[order(names(is2_coef))]
is3_coef <- is3_coef[order(names(is3_coef))]


is1_tib <- data.frame(name = c("fesisFinland.2000",
"fesisGermany.2002",
"fesisIreland.2011",
"fesisIreland.2015",
"fesisLuxembourg.2007",
"fesisSweden.2001"),
coef = c(-0.103,
-0.105,
-0.087,
-0.148,
-0.136,
-0.095))


is2_tib <- data.frame(name = c("fesisFinland.2000",
"fesisGermany.2002",
"fesisIreland.2015",
"fesisSweden.2001"),
coef = c(-0.123,
-0.131,
-0.192,
-0.103))


is3_tib <- data.frame(name = c("fesisFinland.2000",
"fesisGermany.2002",
"fesisIreland.2011",
"fesisLuxembourg.2015",
"fesisSweden.2001"),
coef = c(-0.128,
-0.108,
-0.127,
-0.214,
-0.110))



# Check that Equal Breaks as in Koch et al are identified

is1_tib_estimated <- data.frame(name = names(is1_coef[is1_coef < 0]),
coef = as.numeric(round(is1_coef[is1_coef < 0], 3)))
Expand All @@ -160,11 +164,9 @@ test_that("Equal Breaks as in Koch et al are identified",{

expect_equal(is3_tib, is3_tib_estimated)

})



test_that("Equal Break Uncertainties as in Koch et al are identified",{
# Check that Equal Break Uncertainties as in Koch et al are identified"

break_is1 <- break_uncertainty( is1, interval = 0.99)
break_is2 <- break_uncertainty( is2, interval = 0.99)
Expand Down Expand Up @@ -221,10 +223,10 @@ test_that("Equal Break Uncertainties as in Koch et al are identified",{
expect_equal(is2_tib_break, break_is2[,c("name", "tci")])
expect_equal(is3_tib_break, break_is3[,c("name", "tci")])

})


test_that("Koch et al: Standard Error Corrections",{

# Check that Koch et al: Standard Error Corrections are correct

is1_robust_nocluster <- robust_isatpanel(is1, cluster = FALSE)$robust
is2_robust_nocluster <- robust_isatpanel(is2, cluster = FALSE)$robust
Expand Down

0 comments on commit f0a2d33

Please sign in to comment.