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
## Check whether columns could be a key to a data.frame | |
## | |
## @param x \code{data.frame} to check | |
## @param i \code{character} or \code{integer} Column names | |
## @return \code{logical}. \code{TRUE} if no duplicated values | |
## of \code{i} in \code{x}. | |
is_key <- function(x, i) { | |
!any(duplicated(x[ , i])) | |
} |
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
library(ggplot2) | |
library(plyr) | |
library(grid) | |
notnull <- function(x) !is.null(x) | |
as.character.element <- function(x) { | |
.f <- function(x) { | |
if (class(x) %in% c("rel", "unit")) { | |
as.character(x) |
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
setOldClass("dlm") | |
setOldClass("dlmFiltered") | |
##' Mean Squared Error Generic function | |
mse <- function(x, na.rm=FALSE, ...) { | |
mean((x - mean(x, na.rm=na.rm))^2, na.rm=na.rm) | |
} | |
##' @export | |
setGeneric("mse") |
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
##' Bayesian normal linear regression with reference prior | |
##' | |
##' Given an lm object, simulate the Bayesian posterior. | |
##' | |
##' @export | |
bayeslm <- function(..., niter=100) { | |
llik <- function(y, b, sigma2, X) { | |
sum(dnorm(y, X %*% b, sqrt(sigma2), log = TRUE)) | |
} |
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
For ($i=1; $i -le 4; $i++) { | |
model --refresh=0 --seed=12345 --chain_id=$i --samples=samples$i.csv | |
} |
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
library(KFAS) | |
library(rstan) | |
data(GlobalTemp) | |
model_dlm1a <- stan_model("../stan/dlm1a.stan") | |
y <- as.matrix(GlobalTemp) | |
data <- | |
within(list(), | |
{ |
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
data { | |
int n; | |
int m; | |
row_vector[m] alpha; | |
} | |
parameters { | |
row_vector[m] beta[n-1]; | |
} | |
transformed parameters { | |
matrix[n, m] alpha_beta; |
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
library("stringr") | |
library("ggplot2") | |
shinyServer(function(input, output) { | |
output$main_plot <- | |
renderPlot({ | |
x <- as.numeric(str_split(input$data, ",")[[1]]) | |
xseq <- seq(min(x), max(x), length.out=100) | |
xvar <- var(x) |
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
quantile.ordered <- function(x, probs = seq(0, 1, 0.25)) { | |
tab <- table(x) | |
cdf <- cumsum(tab / sum(tab)) | |
idx <- sapply(probs, function(p) min(which(cdf >= p))) | |
levels(x)[idx] | |
} | |
quantile.factor <- quantile.ordinal | |
median.ordered <- function(x) quantile(x, 0.5) | |
median.factor <- median.ordered |
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
library("ggplot2") | |
#' Draw Normal Distribution Density with an area shaded in. | |
#' | |
#' @param lb Lower bound of the shaded area. Use \code{-Inf} for a left tail. | |
#' @param ub Upper bound of the shaded area. Use \code{Inf} for a right tail. | |
#' @param mean Mean of the normal distribution | |
#' @param sd Standard deviation of the normal distribution | |
#' @param limits Lower and upper bounds on the x-axis of the area displayed. | |
#' @return ggplot object. |