File: Chapter.7.2.R

package info (click to toggle)
r-cran-learnbayes 2.15.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,864 kB
  • sloc: sh: 15; makefile: 2
file content (28 lines) | stat: -rw-r--r-- 787 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#####################################################
#  Section 7.2  Introduction to Hierarchical Modeling
#####################################################

library(LearnBayes)
library(lattice)

data(sluggerdata)

# fit logistic model for home run data for a particular player

logistic.fit=function(player)
{
d=subset(sluggerdata,Player==player)
x=d$Age; x2=d$Age^2
response=cbind(d$HR, d$AB-d$HR)
list(Age=x, p=glm(response~x+x2,family=binomial)$fitted)
}

names=unique(sluggerdata$Player); newdata=NULL
for (j in 1:9)
{
  fit=logistic.fit(as.character(names[j]))
  newdata=rbind(newdata,data.frame(as.character(names[j]),fit$Age,fit$p))
}
names(newdata)=c("Player","Age","Fitted")
xyplot(Fitted~Age|Player, data=newdata, type="l",lwd=3,col="black")