Skip to content

Instantly share code, notes, and snippets.

Avatar

Jonas Moss JonasMoss

View GitHub Profile
@JonasMoss
JonasMoss / tmvtnorm_correlation_EGD.R
Created Nov 29, 2017
Using tmvtnorm and parametric bootstrap to calculate the correlation coefficient in extreme group design.
View tmvtnorm_correlation_EGD.R
## 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 / skewness.R
Created Dec 4, 2017
A demonstration that skewness in both the dependent and independent variables is possible in linear regression.
View skewness.R
## 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 / selection_for_significance.ipynb
Last active Dec 4, 2017
Selection for significance.
View selection_for_significance.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@JonasMoss
JonasMoss / trust_simulation.R
Created Jan 2, 2018
Attempt to simulate trust data.
View trust_simulation.R
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)
View SSC_posting_and_politics.R
## 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.
View SSC_posting_and_politics_beta.R
## 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 Feb 13, 2018
Sampling from a Gaussian kernel density estimate in R.
View kde_sampling_gaussian_kernel.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 Feb 13, 2018
Sampling from an Epanechnikov kernel density estimate in R.
View kde_sampling_epanechnikov_kernel.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 / functor_memoization.R
Created Feb 18, 2018
Using functors to memoize a function in R.
View functor_memoization.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
@JonasMoss
JonasMoss / functors_in_R.R
Last active Feb 19, 2018
C++-style functors in R.
View functors_in_R.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.