This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
multiple_lags <- function(data = ., | |
execute_on_variables = vars(everything()), | |
lag_vector = c(1)){ | |
intermediate <- tibble(data) | |
for(i in lag_vector){ | |
data %>% | |
# Lags - note we don't include the dependent variable because we want to regulate this using the ARX object | |
mutate_at(.vars = execute_on_variables, .funs = list(lagged = ~lag(x=.,n = i))) %>% | |
ungroup() %>% | |
select(ends_with("_lagged")) %>% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Multiple differences function | |
# dplyr package needed | |
multiple_differences <- function(data = ., | |
execute_on_variables = vars(everything()), | |
difference_vector = c(1)){ | |
intermediate <- tibble(data) | |
for(i in difference_vector){ | |
data %>% | |
mutate_at(.vars = execute_on_variables, .funs = list(differenced = ~ . - lag(x=.,n = i))) %>% | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# based on https://stat.ethz.ch/pipermail/r-help/2006-October/115706.html | |
# Assuming we have a matrix like: | |
mat <- structure(c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, | |
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, | |
0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, | |
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, | |
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, | |
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Currently this is implemented for numeric vectors (e.g. c(1,2) and c(1:5)), character vectors, lists and logical arguments. | |
# Further work might be needed, depending on the function arguments | |
extract_function_arguments <- function(myfunction) { | |
obj <- as.list(formals(myfunction)) | |
lapply(seq_along(obj), function(i) { | |
assign(names(obj)[i], | |
if (class(obj[[i]]) == "call") { | |
if (all(as.character(obj[[i]])[-1] %in% c(0:9))) { | |
if (as.character(obj[[i]])[1] == ":") { | |
as.numeric(as.character(obj[[i]])[2]):as.numeric(as.character(obj[[i]])[3]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Original source: https://www.thetopsites.net/article/54022279.shtml | |
resizebox.stargazer = function(..., tab.width = "!", tab.height = "!" | |
){ | |
#Activate str_which() function: | |
require(stringr) | |
#Extract the code returned from stargazer() | |
res = capture.output( | |
stargazer::stargazer(...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
data.frame(t = seq(0, 2 * pi, by = 0.1)) %>% | |
mutate(across(.cols = t, | |
.fns = list(y=~13*cos(.)-5*cos(2*.)-2*cos(3*.)-cos(4*.),x = ~16*sin(.)^3))) %>% | |
ggplot(aes(x=t_x,y=t_y)) + | |
geom_polygon(fill="red3") + | |
labs(x=NULL,y=NULL) + | |
theme(panel.background = element_blank(), | |
axis.text = element_blank(), | |
axis.ticks = element_blank()) + |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lapply(ls(), function(o) assign(x = o, value = get(o), envir = .GlobalEnv)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(plm) | |
library(tidyverse) | |
library(fastDummies) | |
data(EmplUK) | |
EmplUK %>% | |
select(-sector) %>% | |
dummy_cols(.data = .,select_columns = c("firm","year"),remove_selected_columns = TRUE,remove_first_dummy = TRUE) -> paneldata | |
head(paneldata) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
logLik.plm <- function(object){ | |
out <- -plm::nobs(object) * log(2 * var(object$residuals) * pi)/2 - deviance(object)/(2 * var(object$residuals)) | |
attr(out,"df") <- nobs(object) - object$df.residual | |
attr(out,"nobs") <- plm::nobs(object) | |
return(out) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# to get to the recover list - allows you to walk through all frames | |
options(error = recover) | |
# to get back from a call to the options page again, use `c` | |
# just loading any library to try these functions out | |
library(gets) | |
# trace will execute any function when you execute the first function | |
# e.g. it will execute `browser` when I execute `getsm` | |
trace(getsm, browser) |
OlderNewer