Skip to content

Instantly share code, notes, and snippets.

@nozma
Created July 30, 2016 10:55
Show Gist options
  • Select an option

  • Save nozma/04a504c044112b17dc23bdd25d9b967d to your computer and use it in GitHub Desktop.

Select an option

Save nozma/04a504c044112b17dc23bdd25d9b967d to your computer and use it in GitHub Desktop.
カテゴリカルデータ解析第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") +
geom_errorbar(aes(ymin = pred$fit - pred$se.fit,
ymax = pred$fit + pred$se.fit),
width = 0.1) +
theme_bw(base_family = "HiraKakuProN-W3")
# (b)
# データはe-Statから取得したH21.10.1時点の総人口
pop <- c(5053, 852, 1430, 1814, 1195, 1132, 1708, 1382)
result <- glm(x ~ pref, offset = log(pop), 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") +
geom_errorbar(aes(ymin = pred$fit - pred$se.fit,
ymax = pred$fit + pred$se.fit),
width = 0.1) +
theme_bw(base_family = "HiraKakuProN-W3")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment