Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

library(lattice)
key <- list(
rep=FALSE,
lines=list(col=c("#00526D", "blue"), type=c("p","l"), pch=1),
text=list(lab=c("Observation","Estimate")),
rectangles = list(col=adjustcolor("yellow", alpha.f=0.5), border="grey"),
text=list(lab="95% Prediction credible interval"))
xyplot(l.95..CI + u.95..CI + Estimate + Units_sold ~ Temperature | Model,
data=modelData, as.table=TRUE, main="Ice cream model comparision",
xlab="Temperatures (C)", ylab="Units sold",
modelData <- data.frame(
Model=factor(c(rep("Linear model", n),
rep("Log-transformed LM", n),
rep("Poisson (log)",n),
rep("Binomial (logit)",n)),
levels=c("Linear model",
"Log-transformed LM",
"Poisson (log)",
"Binomial (logit)"),
ordered = TRUE),
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
temp <- c(11.9,14.2,15.2,16.4,17.2,18.1,18.5,19.4,22.1,22.6,23.4,25.1)
units <- c(185L,215L,332L,325L,408L,421L,406L,412L,522L,445L,544L,614L)
log_units <- log(units)
n <- length(units)
market.size <- rep(800, n)
# http://www.theguardian.com/news/reality-check/2014/jan/31/sex-guardian-readers-confess-all
# Source: https://docs.google.com/spreadsheet/ccc?key=0At6CC4x_yBnMdDduelJocVo5RDZzalltd0dSQzdXUmc&usp=sharing#gid=0
dat <- data.frame(AgeGroup=factor(
1:9,
labels= c("Younger than 16", "16-24",
"25-34", "35-44", "45-54",
"55-64", "65-74",
"Older than 74", "(blank)"),
ordered=TRUE),
No=c(34, 2079, 2585, 1593,
with(dat, {
prop.test(Yes, Yes + No)
})
#
# 9-sample test for equality of proportions without continuity correction
#
# data: Yes out of Yes + No
# X-squared = 198.4, df = 8, p-value < 2.2e-16
# alternative hypothesis: two.sided
# sample estimates:
set.seed(12345)
dat <- data.frame(category=rep(c("Food", "Drinks"),20),
product=rep(c("Cheese", "Wine", "Bread", "Beer"), 10),
year=sort(rep(c(2004:2013),4)),
value=sort(rnorm(40)))
dat <- dat[with(dat, order(category, year)),]
library(lattice)
# Change some of the default lattice settings
set.seed(1234)
dat <- data.frame(
product=c(rep("Mobile",2),
rep(c("Smartphone", "Mobile"),3),
rep(c("Video rental shop"),3),
rep(c("Video rental shop",
"Online movie rental"),2)),
year=c(2008:2010, 2010, 2011, 2011, 2012, 2012,
2008:2010, 2011, 2011, 2012, 2012),
category=c(rep("Phone", 8), rep("Film", 7)),
library(data.table)
dt <- data.table(dat, key="product,year")
# Create a table that list all years for all products.
py <- CJ(product=levels(dt[, product]),
year=unique(dt[,year]))
# Create a mapping table from old to new products.
# The new products are named product, so that I can
# join them with the original data and get a new colulmn
# with the old product category
mapping <- data.table(
dat <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)),
Name=c("Washington", "Adams", "Jefferson",
"Adams", "Jefferson", "Burr"),
start=as.Date(x=rep(c("1789-03-29", "1797-02-03",
"1801-02-03"),2)),
end=as.Date(x=rep(c("1797-02-03", "1801-02-03",
"1809-02-03"),2)))
plot(
gvisTimeline(data=dat, rowlabel="Name",barlabel="Position",
start="start", end="end",