Skip to content

Instantly share code, notes, and snippets.

set.seed(123)
x<- runif(100)
y<- 2*x + rnorm(100,0,0.1)
plot(x,y)
plot(y~x)
summary(lm(y~x))
anova(lm(y~x))
abline(lm(y~x))
lm.devo<- lm(y~x)
@nashutoing
nashutoing / many dendrogram for Ce-league& Pa-league
Created April 2, 2019 10:14
dendrogram for Central league and Pacific league
NPB18cebaZ<- scale(NPB18ceba[4:25]) #standardization
distZ<- data.frame(NPB18cebaZ) #?
distZ<- dist(NPB18cebaZ) #dcalculate distance,default method =euclidean
hclust(distZ) #clustering
summary(hclust(distZ))
plot(hclust(distZ)) #dendrogram
a<- hclust(distZ,method = "average")
hclust(distZ)$merge
par(mfrow=c(1,2)) #to compare
plot(hclust(distZ),main = "default complete:最遠隣法")
@nashutoing
nashutoing / prcomp for MLB18_batter
Created March 31, 2019 13:04
Principal Component Analysis for MLB2018 batter
library(dplyr) # to use select
MB<- select(MB18lim,G:SO) #select the columns(G~SO)
MB18limZ<- scale(MB) #standardization
prr<- prcomp(MB18limZ) # to use Principal component Analysis
prr
summary(prr)
biplot(prr)
prr$rotation #固有ベクトル・
fcl<- sweep(prr$rotation,MARGIN =2, prr$sdev, FUN="*" )
x<- c("G","PA","AB","R","H","X2B","X3B","HR","RBI","SB","CS","BB","SO")
@nashutoing
nashutoing / cor for H-AB and H-Age
Created March 28, 2019 06:10
MLB correlation coefficient ~positive for H and AB , No relation for Age and H~
plot(MB18lim$H,MB18lim$AB)
mean(MB18lim$H)
abline(v=mean(MB18lim$H))
mean(MB18lim$AB)
abline(h=mean(MB18lim$AB))
mtext("平均",side=1,line=1,at=84)
mtext("平均",side=2,line=2,at=333)
text("+",x=130,y=550,cex=5)
text("⁻",x=130,y=180,cex=8)
text("⁻",x=50,y=550,cex=8)
@nashutoing
nashutoing / 2 Q-Qplot for 2018MLB hit
Created March 20, 2019 09:23
Q-Qplot as for the number of hit at(BAT) 2018MLB
MB18<- read.csv("2018MLB_batter.csv")
MB18$H
hist(MB18$H) #distorted, not simillar with normal distribution
MB18lim<- MB18[MB18$H>20,] #linit the player: under 20Hits are deleted
hist(MB18lim$H)
MB18O<- MB18[order(MB18$H),] # aort by ascending order as hit number
MMM<- MB18lim[order(MB18lim$H),] #〃
@nashutoing
nashutoing / Q-Qplot success.ver
Created March 20, 2019 03:15
Q-Qplot for rnorm(100) and normal distribution
rn<- rnorm(100) #make 100 random numbers followed by N(0,1)
rns<- sort(rn) #sort or order by ascending order(essential?)
plot(rns) #No useful(x-axis is represented as index)
rnss<- ecdf(rns) #ready for making cumlative function
par(mfrow=c(1,3)) #divide to set 3 graphs
plot(rnss) #cumlative function for rnorm(100)
abline(h=0.4) #draw horizon=0.4 line
curve(pnorm(x,0,1),xlim=c(-3,3))
abline(h=0.4) #pnorm=cumlative function for normal distribution
@nashutoing
nashutoing / qqnorm_rnorm(100)
Created March 19, 2019 09:44
qqplot_qqnorm as for rnorm(100)
rn<- rnorm(100) #make 100 random numbers followed by N(0,1)
rns<- sort(rn) #sort or order by ascending order(essential?)
plot(rns) #No useful(x-axis and y-axis is opposite)
rnss<- ecdf(rns) #ready for making cumlative function
par(mfrow=c(1,3)) #divide to set 3 graphs
plot(rnss) #cumlative function for rnorm(100)
curve(pnorm(x,0,1),xlim=c(-3,3)) #pnorm=cumlative function for normal distribution
qqnorm(rn) #make qqplot
@nashutoing
nashutoing / aoki_500bats
Created March 17, 2019 06:06
aoki_500bats
aa<- rbinom(1000,500,0.285)
bb<- aa/500
hist(bb,freq=TRUE,breaks = seq(0.22,0.35,0.001),xlab = "打率",ylab = "シーズン数",
main="500打席に立った青木選手の1000シーズン分の打率分布")
pp<- table(bb)
qq<- t(pp)
print(xtable(qq),type="html")
@nashutoing
nashutoing / aoki_100bats
Created March 17, 2019 06:03
aoki_100bats added ways to change rows and comns
a<- rbinom(1000,100,0.3)
b<- a/100
hist(b,freq=TRUE,breaks=seq(0.1,0.5,0.005),xlab = "打率",ylab = "シーズン数",
main="100打席に立った青木選手の1000シーズン分の打率分布")
print(xtable(table(b)),type="html") #didn't use (too long)
p<- table(b)
p
q<- t(p) #change rows and columns
q
print(xtable(q),type="html")
@nashutoing
nashutoing / aoki_10bats
Created March 17, 2019 01:12
aoki_10bats added way to make table for html style and new csv file
a<- rbinom(1000,10,0.285)
打率<- a/10
hist(打率,freq=TRUE,breaks=seq(0,1,0.05),xlab = "打率", ylab = "シーズン数",
main = "10打席に立った青木選手の1000シーズン分の打率分布")
table(打率)
print(xtable(table(打率)),type = "html") #make table(打率) written by html style
write.csv(table(b),"aoki10bat.csv",quote = FALSE, sep=",") #make new table(b).csv file