Skip to content

Instantly share code, notes, and snippets.

View hannesdatta's full-sized avatar
🚩
https://tilburgsciencehub.com

Hannes Datta hannesdatta

🚩
https://tilburgsciencehub.com
View GitHub Profile
@hannesdatta
hannesdatta / save_by_regex.R
Created December 2, 2020 07:29
Save all objects that match a regular expression in .RData file
# Function scans global environment for occurence of regular expression (`regex`),
# and saves all objects in `filename`.
save_by_regex <- function(regex, filename) {
lscall = ls(envir=.GlobalEnv)
stuff_to_save = grep(regex, lscall, value=T)
if (length(stuff_to_save)>0) {
cat('saving...\n')
cat(paste0('(', paste0(stuff_to_save, collapse=', '), ')\n'))
save(list=stuff_to_save , file = filename)
cat('...done.\n') } else {
@hannesdatta
hannesdatta / compare_regression_results.R
Last active December 16, 2020 15:02
Compare regression results with stargazer, and compare coefficients using the delta method (in R)
# Code snippet to compare the outcome of different regressions
# - showing regression results in a publication-ready table
# - conducting a statistical test for differences in coefficients using the delta method
## Let's generate some data
set.seed(1234) # initialize random number generator
y= runif(1000)
x1= runif(1000)
x2= runif(1000)
x3= runif(1000)
@hannesdatta
hannesdatta / draw.R
Created March 1, 2021 12:11
Draw from four-dimensional normal distribution with specific correlation structure
#' Draw from variance-covariance matrix, given correlation rho (buggy!)
#'
#' The purpose of this function is to generate draws from
#' the variance-covariance matrix, given a specific correlation structure.
#' In particular, a four-dimensional correlation structure is drawn, in which
#' the first dimension (sales) is correlated with the remaining 3 dimensions
#' (marketing mix instruments) with *rho*, and the correlations among the 3 dimensions
#' (marketing mix instruments) is zero (or about zero).
#'
#' @param rho Correlation between the first dimension, and the remaining three dimensions
@hannesdatta
hannesdatta / install_packages.R
Created June 25, 2021 07:48
automatically install all R packages used in a project (scans all source code files)
################################
# FIND AND INSTALL R PACKAGES #
# #
# #
# Searches source code for #
# references to packages, #
# and installs all #
# uninstalled packages. #
# #
# Put this script in the #
@hannesdatta
hannesdatta / activity3.R
Created September 23, 2021 12:04
solution_dprep_activity3
# Load packages
library(dplyr)
library(tidyverse)
library(writexl)
#########
# Input #
#########
## Open data
# Load packages
library(tidyverse)
library(reshape2)
# DOWNLOAD DATA
## Function to download data and save as CSV
download_data <- function(url, filename){
download.file(url = url, destfile = paste0(filename, ".csv"))
}
@hannesdatta
hannesdatta / empty-data.R
Created November 1, 2021 13:44
create empty data frame to prepare household-level panel data
library(data.table)
time = 1:200
stores = c('ah', 'jumbo', 'super')
panelists = paste0('id', 1:1000)
store_id = rep(stores, each=length(time))
time_id = rep(time, length(stores))
year = floor(time_id/52)+1
@hannesdatta
hannesdatta / models.R
Created January 11, 2022 11:11
Handling memory issues in R: terminal use, arguments, storing model objects by regular expressions
# HOW TO MANAGE MEMORY ISSUES IN R?
# A common problem of data- and computation-intensive projects
# in R is memory management.
# Suppose you would like to estimate a series of models,
# but estimating all of them would exceed your available
# memory.
#
# One solution could be to have individual R scripts
@hannesdatta
hannesdatta / inside_airbnb.R
Created February 17, 2022 15:15
R code to download multiple data sets from InsideAirBnB
# Looping
for (i in 1:10) {
print(i)
}
# Using looping with return values (i.e., to "save" stuff to carry on working)
results = lapply(1:10, function(x) x*2)
# Demo to download all of Europe's listing data to R
library(googledrive)
@hannesdatta
hannesdatta / makefile
Created March 10, 2022 06:29
Use R in a makefile to clean-up (temporary) directories
# Cleanup, wiping the (sub)directories output/temp/audit
wipe:
R -e "unlink('../../gen/data-preparation/output/*.*')"
R -e "unlink('../../gen/data-preparation/temp/*.*')"
R -e "unlink('../../gen/data-preparation/audit/*.*')"