Skip to content

Instantly share code, notes, and snippets.

@nozma
nozma / Sec6setup.R
Created July 5, 2016 15:08
Rプログラミング入門第6章用準備ファイル
# read "deck" dataframe
deck <- read.csv("https://gist.githubusercontent.com/garrettgman/9629323/raw/ee5dfc039fd581cb467cc69c226ea2524913c3d8/deck.csv")
DECK <- deck
# define functions
roll <- function(){
die <- 1:6
dice <- sample(die, size = 2, replace = TRUE)
sum(dice)
}
@nozma
nozma / Sec2ans.R
Created July 24, 2016 16:07
カテゴリカルデータ解析第2章練習問題解答
# 1.
# (a)
CtableS <- xtabs(~ Sex, data = Arthritis)
barplot(CtableS)
pie(CtableS, clockwise = TRUE)
# (b)
mosaicplot(~ Sex + Improved, data = Arthritis, col = TRUE)
# 3.
xtabs(Freq ~ management + own, data = JobSatisfaction)
@nozma
nozma / Sec3ans.R
Created July 24, 2016 16:35
カテゴリカルデータ解析第3章練習問題解答
# 練習問題
# 1.
binom.test(134, 200)$conf.int
# 2.
binom.test(145, 300, 0.3)
# 3.
p <- c(0.382, 0.219, 0.305, 0.094)
x <- c(39, 16, 27, 15)
@nozma
nozma / Sec4ans.R
Created July 25, 2016 13:44
カテゴリカルデータ解析第4章練習問題解答
# 練習問題
# 1.
Ctable <- xtabs(Freq ~ management + own, data = JobSatisfaction)
# (a)
chisq.test(Ctable)
# (b)
fisher.test(Ctable)
# 2.
Ctable <- xtabs(Freq ~ Sex + Survived, data = Titanic[,,"Child",])
@nozma
nozma / Sec5ans.R
Created July 26, 2016 13:29
カテゴリカルデータ解析第5章練習問題解答
# 練習問題
# 1.
# (a) (b)
mantelhaen.test(CoalMiners)
# (c)
library(epiDisplay)
mhor(mhtable = CoalMiners)
# 2.
@nozma
nozma / Sec6ans.R
Last active July 27, 2016 14:47
カテゴリカルデータ解析第6章練習問題解答
# 練習問題
# 1.
Titan <- data.frame(Titanic)
res <- glm(Survived ~ Class + Sex + Age, weights = Freq,
family = binomial, data = Titan)
res
summary(res)
exp(res$coefficients)
mosaicplot(Titanic, color = TRUE)
@nozma
nozma / Sec7ans.R
Created July 30, 2016 10:55
カテゴリカルデータ解析第7章練習問題解答
# 練習問題
# (a)
x <- c(314, 39, 78, 105, 77, 93, 140, 69)
pref <- c("A福岡", "B佐賀", "C長崎", "D熊本",
"E大分", "F宮崎", "G鹿児島", "H沖縄")
result <- glm(x ~ pref, family = poisson)
pred <- predict(result, type = "terms",
se.fit = TRUE)
ggplot(data.frame(pref, pred$fit), aes(x = pref, y = pred$fit)) +
geom_bar(stat = "identity") +
@nozma
nozma / Sec8ans.R
Created July 30, 2016 14:01
カテゴリカルデータ解析第8章練習問題解答
# 練習問題
# 1.
JointSports
result <- loglm(Freq ~ opinion * year * grade * gender, data = JointSports)
step(result)
step(result, k = log(sum(JointSports$Freq)))
mosaicplot(xtabs(Freq ~ year + grade + gender + opinion, data = JointSports))
# 2.
# (a)
tabledata <- xtabs(Freq ~ sex + method2 + age.group, data = Suicide)
@nozma
nozma / Sec10ans.R
Created August 7, 2016 13:01
カテゴリカルデータ解析第10章練習問題解答
# 1.
data("JointSports")
result <- ctree(opinion ~ year + grade + gender, data = JointSports,
weights = Freq)
plot(result)
# 2.
titan.d <- data.frame(Titanic)
head(titan.d)
result <- ctree(Survived ~ Class + Sex + Age, weights = Freq,
data = titan.d)
@nozma
nozma / file0.r
Created January 22, 2017 13:13
ggplot2で分割したグラフ毎に異なるtableGrobを書き込む ref: http://qiita.com/nozma/items/a87bbca80b77b3abd161
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~drv) +
theme_bw() +
theme(panel.grid = element_blank()) +
geom_smooth(method = "lm")