title: Activity15" output: htmlnotebook ---

{r} # Read in data baseball = read.csv("baseball.csv") str(baseball) {r} #Each row in the baseball dataset represents a team in a particular year.How many team/year pairs are there in the whole dataset? nrow(baseball) {r} #Though the dataset contains data from 1962 until 2012, we removed several years with shorter-than-usual seasons. Using the table() function, identify the total number of years included in this dataset. table(baseball$Year)

{r} #The baseball dataset contains 47 years (1972, 1981, 1994, and 1995 are missing). We can count the number of years in the table, or use the command length(table(baseball$Year)) directly to get the answer. length(table(baseball$Year))

{r} baseball = subset(baseball, Playoffs == 1) nrow(baseball)

{r} #Through the years, different numbers of teams have been invited to the playoffs. table(baseball$Year)

{r} table(table(baseball$Year))

```{r}

We start by storing the output of the table() function that counts the number of playoff teams from each year:

PlayoffTable = table(baseball$Year)

You can output the table with the following command:

PlayoffTable ```

{r} str(names(PlayoffTable))

{r} #Which function call returns the number of playoff teams in 1990 and 2001? PlayoffTable[c("1990", "2001")]

{r} #Putting it all together, we want to look up the number of teams in the playoffs for each team/year pair in the dataset, and store it as a new variable named NumCompetitors in the baseball data frame. baseball$NumCompetitors = PlayoffTable[as.character(baseball$Year)] baseball$NumCompetitors

{r} #How many playoff team/year pairs are there in our dataset from years where 8 teams were invited to the playoffs? table(baseball$NumCompetitors)

{r} #How many observations do we have in our dataset where a team did NOT win the World Series? baseball$WorldSeries = as.numeric(baseball$RankPlayoffs == 1) table(baseball$WorldSeries)

{r} #Which of the following variables is a significant predictor of the WorldSeries variable in a bivariate logistic regression model? #Varibales to use as predictors for each bivariate model(Year, RS, RA, W, OBP, SLG, BA, RankSeason, OOBP,OSLG, NumCompetitors, League) model1<-glm(WorldSeries~Year, data=baseball, family="binomial") summary(model1) Year is a significant predictor!

{r} model2<-glm(WorldSeries~RS, data=baseball, family="binomial") summary(model2)

RS is not significant

{r} model3<-glm(WorldSeries~RA, data=baseball, family="binomial") summary(model3)

RA is a significant predictor

{r} model4<-glm(WorldSeries~W, data=baseball, family="binomial") summary(model4)

W is not significant

{r} model5<-glm(WorldSeries~OBP, data=baseball, family="binomial") summary(model5) OBP is not significant

{r} model6<-glm(WorldSeries~SLG, data=baseball, family="binomial") summary(model6)

SLG is not significant

{r} model7<-glm(WorldSeries~BA, data=baseball, family="binomial") summary(model7)

BA is not significant

{r} model8<-glm(WorldSeries~RankSeason, data=baseball, family="binomial") summary(model8)

RankSeason is significant!

{r} model9<-glm(WorldSeries~OOBP, data=baseball, family="binomial") summary(model9)

OOBP is not significant

{r} model10<-glm(WorldSeries~OSLG, data=baseball, family="binomial") summary(model10)

OSLG is not significant

{r} model11<-glm(WorldSeries~NumCompetitors, data=baseball, family="binomial") summary(model11)

NumCompetitors is significant

{r} model12<-glm(WorldSeries~League, data=baseball, family="binomial") summary(model12)

Multivariate Models for Predicting World Series Winner

{r} #How many variables are significant in the combined model? LogModel = glm(WorldSeries ~ Year + RA + RankSeason + NumCompetitors, data=baseball, family=binomial) summary(LogModel)

{r} #We can compute all pair-wise correlations between these variables with: cor(baseball[c("Year", "RA", "RankSeason", "NumCompetitors")])

```{r}

The two-variable models can be built with the following commands:

model13 = glm(WorldSeries ~ Year + RA, data=baseball, family=binomial) summary(model13) ```

{r} model14 = glm(WorldSeries ~ Year + RankSeason, data=baseball, family=binomial) summary(model14)

{r} model15 = glm(WorldSeries ~ Year + NumCompetitors, data=baseball, family=binomial) summary(model15)

{r} model16 = glm(WorldSeries ~ RA + RankSeason, data=baseball, family=binomial) summary(model16)

{r} model17 = glm(WorldSeries ~ RA + NumCompetitors, data=baseball, family=binomial) summary(model17)

{r} model18 = glm(WorldSeries ~ RankSeason + NumCompetitors, data=baseball, family=binomial) summary(model18)