Skip to content

Instantly share code, notes, and snippets.

# Original code by Max Gordon
# Source: http://gforge.se/2013/06/visualizing-transitions-with-the-transitionplot-function/
# Create the transition matrix
library(Gmisc)
library(grid)
library(RColorBrewer)
set.seed(9730)
b4 <- sample(1:3, replace = TRUE, size = 500, prob = c(0.1, 0.4, 0.5))
after <- sample(1:3, replace = TRUE, size = 500, prob = c(0.3, 0.5, 0.2))
b4 <- factor(b4, labels = c("None", "Moderate", "Major"))
library(formattable)
DF <- data.frame(Ticker=c("", "", "", "IBM", "AAPL", "MSFT"),
Name=c("Dow Jones", "S&P 500", "Technology",
"IBM", "Apple", "Microsoft"),
Value=accounting(c(15988.08, 1880.33, NA,
130.00, 97.05, 50.99)),
Change=percent(c(-0.0239, -0.0216, 0.021,
-0.0219, -0.0248, -0.0399)))
DF
## Ticker Name Value Change
## Markus Gesmann, March 2012
library(simecol)
library(latticeExtra)
HaresLynxObservations <- "Year Hares.x.1000 Lynx.x.1000
1900 30 4
1901 47.2 6.1
1902 70.2 9.8
1903 77.4 35.2
@mages
mages / LV.R
Created December 3, 2015 17:14
HaresLynxObservations <- structure(
list(Year = 1900:1920,
Hares.x.1000 = c(30, 47.2, 70.2, 77.4,
36.3, 20.6, 18.1, 21.4,
22, 25.4, 27.1, 40.3, 57,
76.6, 52.3, 19.5, 11.2,
7.6, 14.6, 16.2, 24.7),
Lynx.x.1000 = c(4, 6.1, 9.8, 35.2, 59.4,
41.7, 19, 13, 8.3, 9.1, 7.4,
8, 12.3, 19.5, 45.7, 51.1,
stanmodel <- "
data {
int<lower=0> N;
real x[N];
real Y[N];
}
parameters {
real alpha;
real beta;
real<lower=.5,upper= 1> lambda; // original gamma in the JAGS example
dat <- list(
"N" = 27,
"x" =
c(1, 1.5, 1.5, 1.5, 2.5, 4, 5, 5, 7, 8, 8.5, 9, 9.5, 9.5, 10,
12, 12, 13, 13, 14.5, 15.5, 15.5, 16.5, 17, 22.5, 29, 31.5),
"Y" =
c(1.8, 1.85, 1.87, 1.77, 2.02, 2.27, 2.15, 2.26, 2.47, 2.19,
2.26, 2.4, 2.39, 2.41, 2.5, 2.32, 2.32, 2.43, 2.47, 2.56, 2.65,
2.47, 2.64, 2.56, 2.7, 2.72, 2.57))
library(lattice)
library(data.table)
library(arm)
library(ChainLadder)
dat <- data.table( # Page D5.17
originf=factor(rep(0:6, each=7)),
devf=factor(rep(0:6, 7)),
inc.value= c(3511, 3215, 2266, 1712, 1059, 587, 340,
4001, 3702, 2278, 1180, 956, 629, NA,
library(rstan)
rtools <- "C:\\Rtools\\bin"
gcc <- "C:\\Rtools\\gcc-4.6.3\\bin"
path <- strsplit(Sys.getenv("PATH"), ";")[[1]]
new_path <- c(rtools, gcc, path)
new_path <- new_path[!duplicated(tolower(new_path))]
Sys.setenv(PATH = paste(new_path, collapse = ";"))
# The above overcomes:
# Error in compileCode(f, code, language = language, verbose = verbose) :
A <- function(samples){
as.matrix(samples[,c("b_Intercept" ,"b_temp")])
}
x <- c(1, 35)
prob <- 0.975
lin.samples <- posterior_samples(lin.mod)
n <- nrow(lin.samples)
mu <- A(lin.samples) %*% x
sigma <- lin.samples[,"sigma_units"]
(lin.q <- quantile(rnorm(n, mu, sigma), prob))
log.lin.mod
## Family: gaussian (identity)
## Formula: log_units ~ temp
## Data: NULL (Number of observations: 12)
## Samples: 2 chains, each with n.iter = 2000; n.warmup = 500; n.thin = 1;
## total post-warmup samples = 3000
## WAIC: -9.76
##
## Fixed Effects:
## Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat