options(repr.plot.width = 4, repr.plot.height = 4)
stattest1 <- c(6,10,6,10,5,3,5,9,3,3,11,6,11,9,7,5,8,7,7,9)
stattest2 <- c(10,13,8,15,8,6,9,10,7,3,18,14,18,11,12,5,7,12,7,7)
library(ggplot2)
library(pipeR)
d <- data.frame(stattest1, stattest2)
d %>>%
ggplot(aes(x = stattest1, y = stattest2)) +
geom_point()
psychology <- c(13,14,7,12,10,6,8,15,4,14,9,6,10,12,5,12,8,8,12,15)
d$psychology <- psychology
d %>>%
ggplot(aes(x = psychology, y = stattest1)) +
geom_point()
d %>>%
ggplot(aes(x = psychology, y = stattest2)) +
geom_point()
covar <- ((stattest1 - mean(stattest1)) * (stattest2 - mean(stattest2))) %>>% mean
covar
cov
は不偏共分散(n-1 で割る)cov(stattest1, stattest2)
cov(stattest1, stattest2) * (length(stattest1) - 1) / length(stattest1)
height.m <- c(1.5,1.6,1.7,1.8,1.9)
height.cm <- height.m * 100
weight <- c(50,70,60,80,90)
cov(height.m, weight)
cov(height.cm, weight)
$r_{xy} = \frac{s_{xy}}{s_x s_y}$
cov(stattest1, stattest2) / (sd(stattest1) * sd(stattest2))
cor(stattest1, stattest2)
cor(psychology, stattest1)
cor(psychology, stattest2)
math <- c("嫌い","嫌い","好き","好き","嫌い","嫌い","嫌い","嫌い","嫌い", "好き","好き","嫌い","好き","嫌い","嫌い","好き","嫌い","嫌い","嫌い","嫌い")
table(math)
stat <- c("好き","好き","好き","好き","嫌い","嫌い","嫌い","嫌い","嫌い", "嫌い","好き","好き","好き","嫌い","好き","嫌い","嫌い","嫌い","嫌い","嫌い")
table(stat)
table(math, stat)
math.bin <- ifelse(math=="好き",1,0)
math.bin
stat.bin <- ifelse(stat=="好き",1,0)
stat.bin
cor(math.bin, stat.bin)
d.study <- data.frame(time = c(1, 3, 10, 12, 6, 3, 8, 4, 1, 5), score = c(20, 40, 100, 80, 50, 50, 70, 50, 10, 60))
d.study
library(Cairo)
CairoFonts()
で設定するとすべてのFontが置き換わってしまうので,プレビュー用.CairoFonts(regular = "IPAexGothic")
Cairo(type = "raster")
d.study %>>%
ggplot(aes(x = time, y = score)) +
geom_point() +
theme(axis.title = element_text(family="IPAexGothic")) +
xlab("勉強時間") +
ylab("定期試験の得点")
dev.off()
cor(d.study$time, d.study$score)
library(readxl)
d.food <- read_excel("Chap03_food.xlsx", sheet = 1)
str(d.food)
table(d.food)
library(dplyr)
d.food %>>%
mutate(WesternOrJapanese = ifelse(WesternOrJapanese=="和食", 1, 0),
SweetOrHot = ifelse(SweetOrHot == "甘党", 1, 0)) %>>%
summarise(cor(WesternOrJapanese, SweetOrHot))
devtools::session_info()