Skip to content

Instantly share code, notes, and snippets.

View JonasMoss's full-sized avatar

Jonas Moss JonasMoss

View GitHub Profile
@JonasMoss
JonasMoss / tmvtnorm_correlation_EGD.R
Created November 29, 2017 09:59
Using tmvtnorm and parametric bootstrap to calculate the correlation coefficient in extreme group design.
## This script uses the tmvtnorm package, check it out.
if(!("tmvtnorm" %in% rownames(installed.packages()))) {
install.packages("tmvtnorm")
}
library("tmvtnorm")
## Seed for reproducibility.
set.seed(313)
@JonasMoss
JonasMoss / selection_for_significance.ipynb
Last active December 4, 2017 16:06
Selection for significance.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@JonasMoss
JonasMoss / skewness.R
Created December 4, 2017 13:42
A demonstration that skewness in both the dependent and independent variables is possible in linear regression.
## Needed to calculate moments.
library("moments")
set.seed(313)
## An illustration with both variables skewed.
n = 200
x = rbeta(n, 1, 15)
y = 3 + x + rnorm(n, 0, 0.01)
## Plotting!
@JonasMoss
JonasMoss / trust_simulation.R
Created January 2, 2018 20:37
Attempt to simulate trust data.
N = 1000
K = 1
res = MCMCpack::rdirichlet(N, K*c(0.1, 0.15, 0.7, 0.05))
score = rowSums(res^2)
trust = res%*%c(-1,-1,1,0) + rnorm(N, 0, 0.1)
covs = data.frame(afro = res[1,], latios = res[2,], whites = res[3,], asians = res[4,], score = score)
lm(trust ~ ., data = covs)
## Makes a plot checking for clear associations between politics and postign in SSC.
## Data from: http://slatestarcodex.com/Stuff/ssc2018public.xlsx
library("tidyverse")
SSC = read_excel("ssc2018public.xlsx") # Downloaded from SSC.
Politics = as.factor(SSC$PoliticalAffiliation)
Comments = as.factor(SSC$Comment)
## Not everyone will agree with my classification.
## Checks for associations between politics and posting at SSC, using Beta-Bayes!
## Data from: http://slatestarcodex.com/Stuff/ssc2018public.xlsx
library("tidyverse")
SSC = read_excel("ssc2018public.xlsx") # Downloaded from SSC.
Politics = as.factor(SSC$PoliticalAffiliation)
Comments = as.factor(SSC$Comment)
## Not everyone will agree with my classification.
@JonasMoss
JonasMoss / kde_sampling_gaussian_kernel.R
Last active February 13, 2018 17:39
Sampling from a Gaussian kernel density estimate in R.
## Make a function rkde that samples from a kernel density
#' Sample from a kernel density.
#'
#' @param n Number of observations to sample.
#' @param x The data from which the estimate is to be computed.
#' @param bw Desired bandwidth.
#' @return A numeric vector with n sampled data points from the kernel
#' density estimator.
@JonasMoss
JonasMoss / kde_sampling_epanechnikov_kernel.R
Created February 13, 2018 17:52
Sampling from an Epanechnikov kernel density estimate in R.
#' Sample from a kernel density with the Epanechnikov kernel.
#'
#' @param n Number of observations to sample.
#' @param x The data from which the estimate is to be computed.
#' @param bw Desired bandwidth.
#' @return A numeric vector with n sampled data points from the kernel
#' density estimator.
#' @details The factor sqrt(5) in is needed since the standard deviation of
#' the kernel is 1/sqrt(5). This makes the normal kernel and the Epanchnikov
#' kernel comparable.
@JonasMoss
JonasMoss / functors_in_R.R
Last active February 19, 2018 10:48
C++-style functors in R.
## A simple example of a functor in R.
## A simple function of one argument. Since y is neither defined inside the function body or
## given as an argument, R will search the enclosing environment for y. Since R is lazy, it
## won't do this when the function is defined, but only when the function is called (and y
## is needed.)
f = function(x) x^2 + y^2
## This changes the enclosing environment of f to a new environment. It was the global
## environment before, and we don't want that.
@JonasMoss
JonasMoss / functor_memoization.R
Created February 18, 2018 18:12
Using functors to memoize a function in R.
## Functor example 1: Memoization.
## We memoize a function g with up to N values.
## The function to memoize: It is slow to calculate, so we'll gain from
## storing its values.
g = function(x) {
Sys.sleep(0.1)
print("Processesing ...")
x^2