Skip to content

Instantly share code, notes, and snippets.

View moritzpschwarz's full-sized avatar

Moritz Schwarz moritzpschwarz

View GitHub Profile
@moritzpschwarz
moritzpschwarz / multiple_lag_function_dplyr.R
Last active May 28, 2020 15:04
Creates multiple lags using dplyr.
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")) %>%
# 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))) %>%
@moritzpschwarz
moritzpschwarz / reverse_dummies_to_factor.R
Created August 6, 2020 14:52
base level of the factor is not present as a column in the matrix, such that all of the columns would be 0 in the case where the base level is present. This would be the typical result of model.matrix() with default Treatment contrasts. using matrix multiplication, and expanding upon it, would ensure that we have all factors present again.
# 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,
@moritzpschwarz
moritzpschwarz / formals to environment.R
Last active August 11, 2020 14:11
This gist is supposed to quickly copy all formal arguments to the Global Environment. This makes inspecting a function much easier (although I'm sure there are many easier ways to do this).
# 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])
@moritzpschwarz
moritzpschwarz / resizebox.stargazer.R
Last active August 10, 2020 22:37
The `stargazer` arguments can be put inside of this function. Then the output for the PDF is resized e.g. through something like this: `resizebox.stargazer(df,summary = TRUE,header = FALSE,tab.width = "0.9\\textwidth")` Taken from https://www.thetopsites.net/article/54022279.shtml
# 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(...)
@moritzpschwarz
moritzpschwarz / plot_heart.R
Created September 5, 2020 16:35
Spread the love :) <3
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()) +
@moritzpschwarz
moritzpschwarz / save_files_within_function_to_global_env.R
Last active December 21, 2020 04:17
This is a function that can be executed, when using Debugging and being in the browser. This will save all variables that are in the environment of the function into the global environment. The source for this comes from [StackOverflow](https://stackoverflow.com/questions/55809121/is-there-a-way-to-save-variables-in-debugger-mode-to-global-envir…
lapply(ls(), function(o) assign(x = o, value = get(o), envir = .GlobalEnv))
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)
@moritzpschwarz
moritzpschwarz / logLik.plm.R
Last active June 19, 2024 08:27
logLik.plm function
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)
}
@moritzpschwarz
moritzpschwarz / debugging_tools.R
Last active September 19, 2022 10:50
A small collection of debugging tools in R
# 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)