This file contains 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
## 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) |
This file contains 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
## 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! |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
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) |
This file contains 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
## 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. |
This file contains 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
## 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. |
This file contains 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
## 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. |
This file contains 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
## 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 |
This file contains 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
## 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. |
This file contains 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
## These are generic functions for extracting and assigning values to the enclosing | |
## environment of a function. It needs class(f) = "function" to work. | |
`$.function` = function(f, y) environment(f)[[y]] | |
`$<-.function` = function(f, y, value) { | |
environment(f)[[y]] = value | |
invisible(f) | |
} |
OlderNewer