# 2019 認知情報解析学演習　RL01

```set.seed(111)
n.trial = 1000; N = 10; sigma  = 1
Q.star = runif(N); Q = rep(0, N)
count = rep(0,N); Q.cum = rep(0, N)
rew.earned = rep(0,n.trial)
### playing slot-machine
for (i.trial in 1:n.trial){
max.a = max(Q)
max.idx = which(Q == max.a)
if (length(max.idx)>1){
max.idx = sample(max.idx, 1)
}
r.t = rnorm(1, Q.star[max.idx], sd = sigma)
Q.cum[max.idx] = Q.cum[max.idx] + r.t
count[max.idx] = count[max.idx] + 1
Q[max.idx] = Q.cum[max.idx] / count[max.idx]
rew.earned[i.trial] = r.t
}
plot(rew.earned,type='l')
Q
```

# 2019 データ解析基礎論a DAA02

```x<-matrix(1:8, nrow=2)
x<-matrix(1:8, nrow=2,byrow=T)
data01<-data.frame(score = c(2,4,3,4),
dose = c(rep(10,2),rep(100,2)),
condition = rep(c('exp','control'),2))

mean(dat\$shoesize[dat\$gender == "M"])
mean(dat\$shoesize[dat\$gender == "F"])
mean(dat\$shoesize[dat\$h > 180])

v1 = seq(-3,3,0.1)
v2 = v1^2
plot(x = v1, y = v2)
plot(v1, v2, col = 'red')

plot(v1, v2, main = "THIS IS THE TITLE", cex.lab = 1.5,
xlab = "Label for X-axis",ylab = "Label for Y-axis")

plot(v1, v2, col = "blue", type = "o", lty = 2, pch = 19,
cex.lab = 1.5, lwd = 3, main = "Y=X*X", xlab = "X",
ylab="X*X", xlim=c(-3.5,3.5), ylim=c(-0.5, 10))

hist(dat\$h)
hist(dat\$h, breaks = 20, main = “Histogram of Height”,
xlab = "Height", col = 'blue', xlim = c(140, 190))

dens<-density(dat\$h);
hist(dat\$h, main = "Histogram of Height", xlab = "Height",
xlim = c(140,190), probability = T)
lines(dens, lwd = 2, col = ‘red’, lty=2)

plot(v1, v2, col = "blue", type = "l",
pch = 19, cex.lab = 1.5, lwd = 3,
xlab = "X", ylab="f(X)",
xlim=c(-3.5,3.5), ylim=c(-0.5, 10))
lines(v1, v1^3, col='red',lwd = 3)
legend("bottomright", c("x^2","x^3"), col=c('blue','red'), lwd=2)

boxplot(dat\$h ~ dat\$gender,
main="Distribution of Height by Gender",
ylab="Gender", xlab="Height", col=c('blue','cyan'),
ylim=c(140,190), horizontal=T)

interaction.plot(dat\$gender,
dat\$affil,
dat\$h,
pch=c(20,20),
col=c("skyblue","orange"),
xlab="gender", ylab="height",
lwd=3,type='b',cex=2,
trace.label="Affiliation")

hist(dat[dat\$gender=='F',]\$h,
main="Dist. of Height for Female Participants",
xlab="Height", xlim=c(140,190), probability=T)
dens.F = density(dat[dat\$gender=='F',]\$h)
lines(dens.F, col='blue',lwd=2)

hist(dat[dat\$gender==‘M’,]\$h, main=“Dist. of Height for Male
Participants”, xlab=“Height”, xlim=c(140,190),
probability=T,ylim=c(0,0.08))
dens.M = density(dat[dat\$gender=='M',]\$h)
lines(dens.M, col='green', lwd=2)

plot(dat\$shoesize, dat\$h,
main="Relationship b/w shoesize and height",
xlab = 'shoesize’, ylab='height’,
pch=19, col="red")
txt = paste("r =",round(cor(dat\$shoesize,dat\$h), 4))
text(22, 175, txt, cex = 1.5)

abline(h = mean(dat\$h), col='blue');
abline(v = mean(dat\$shoesize), col='green')

plot(dat[dat\$gender=='F',]\$shoesize, dat[dat\$gender=='F',]\$h,
main="Relationship b/w shoesize and height", xlab='shoesize', ylab='height',
cex.lab=1.5, pch=19, col='blue', xlim=c(20,29), ylim=c(140,190))
lines(dat[dat\$gender=='M',]\$shoesize,dat[dat\$gender=='M',]\$h,
type = 'p', pch = 15, col = 'green')
legend("topleft", c('Female','Male'), pch =c(19,15),
col = c('blue','green'), cex = 1.5)
```

# 2019 データ解析基礎論A DAA01

```dat<-data.frame(score=c(78,70,66,76,78,76,88, 76, 76,72,60,72,70,72,84,70),
cond=c(rep('low',8), rep('high',8)))
boxplot(score~cond, col = c("skyblue",'skyblue4'),data=dat)
summary(aov(score ~ cond, data = dat))

plot(ani~otouto, data=dat,pch=20,cex=3,xlab ="score of Otouto", ylab = "score of Ani")
dat.lm <- lm(ani~otouto, data=dat)
abline(dat.lm, col = 'red',lwd = 2.5)

dat.glm <- glm(gender~shoesize,family="binomial",data=dat)
plot(as.numeric(gender)-1~shoesize,data=dat,pch=20,cex=3,ylab="P(Male)")
cf = coef(dat.glm)
temp.x = seq(20,30,0.1)
y = 1/(1+exp(-1*(cf[1]+temp.x*cf[2])))
lines(temp.x,y,col='cyan',lwd=2)

dat.pca <- princomp(dat)
biplot(dat.pca)

dat.cluster=hclust(dist(dat),method="average")
plot(dat.cluster,cex=1.5)

data01<-data.frame(score = c(2,4,3,4),
dose = c(rep(10,2),rep(100,2)),
condition = rep(c('exp','control'),2))