Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yuu-ito/18ed436d93d0f6d68267 to your computer and use it in GitHub Desktop.
Save yuu-ito/18ed436d93d0f6d68267 to your computer and use it in GitHub Desktop.
ベイズ分類のための変量の特徴を見る。
library("magrittr")
library("dplyr")
data(Titanic)
t <- Titanic %>% as.data.frame %>% arrange(Survived,Class,Sex,Age)
t %>%
group_by(Survived) %>%
summarize(ClassCount=sum(Freq)) %>%
group_by %>%
mutate(priprob=ClassCount/sum(ClassCount)) -> a
t %>%
group_by(attr=Class,Survived) %>%
summarize(valClassCount=sum(Freq)) %>%
left_join(.,a) %>%
mutate(pr_attr_bar_class=valClassCount/ClassCount,
priprob_lik=pr_attr_bar_class*priprob) -> b
t %>%
group_by(attr=Sex,Survived) %>%
summarize(valClassCount=sum(Freq)) %>%
left_join(.,a) %>%
mutate(pr_attr_bar_class=valClassCount/ClassCount,
priprob_lik=pr_attr_bar_class*priprob) -> c
t %>%
group_by(attr=Age,Survived) %>%
summarize(valClassCount=sum(Freq)) %>%
left_join(.,a) %>%
mutate(pr_attr_bar_class=valClassCount/ClassCount,
priprob_lik=pr_attr_bar_class*priprob) -> d
b %>%
rbind(c) %>%
rbind(d) %>% as.data.frame -> res
res %>% arrange(desc(priprob_lik))
@yuu-ito
Copy link
Author

yuu-ito commented Jun 10, 2015

   Survived   attr valClassCount ClassCount  priprob pr_attr_bar_class priprob_lik
1        No  Adult          1438       1490 0.676965        0.96510067  0.65333939
2        No   Male          1364       1490 0.676965        0.91543624  0.61971831
3        No   Crew           673       1490 0.676965        0.45167785  0.30577010
4       Yes  Adult           654        711 0.323035        0.91983122  0.29713766
5        No    3rd           528       1490 0.676965        0.35436242  0.23989096
6       Yes   Male           367        711 0.323035        0.51617440  0.16674239
7       Yes Female           344        711 0.323035        0.48382560  0.15629259
8       Yes   Crew           212        711 0.323035        0.29817159  0.09631985
9       Yes    1st           203        711 0.323035        0.28551336  0.09223080
10      Yes    3rd           178        711 0.323035        0.25035162  0.08087233
11       No    2nd           167       1490 0.676965        0.11208054  0.07587460
12       No Female           126       1490 0.676965        0.08456376  0.05724671
13       No    1st           122       1490 0.676965        0.08187919  0.05542935
14      Yes    2nd           118        711 0.323035        0.16596343  0.05361199
15      Yes  Child            57        711 0.323035        0.08016878  0.02589732
16       No  Child            52       1490 0.676965        0.03489933  0.02362562

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment