This file contains hidden or 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
| # 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 { |
This file contains hidden or 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
| # 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) |
This file contains hidden or 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
| #' 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 |
This file contains hidden or 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
| ################################ | |
| # FIND AND INSTALL R PACKAGES # | |
| # # | |
| # # | |
| # Searches source code for # | |
| # references to packages, # | |
| # and installs all # | |
| # uninstalled packages. # | |
| # # | |
| # Put this script in the # |
This file contains hidden or 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
| # Load packages | |
| library(dplyr) | |
| library(tidyverse) | |
| library(writexl) | |
| ######### | |
| # Input # | |
| ######### | |
| ## Open data |
This file contains hidden or 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
| # 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")) | |
| } |
This file contains hidden or 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(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 | |
This file contains hidden or 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
| # 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 |
This file contains hidden or 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
| # 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) |
This file contains hidden or 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
| # 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/*.*')" | |