CAP 4936 // In-class activity #7 : Predicting the Number of Wins

getwd()
# Read in data
baseball = read.csv("baseball.csv")
str(baseball)
# Subset to only include moneyball years
moneyball = subset(baseball, Year < 2002)
str(moneyball)
# Compute Run Difference
moneyball$RD = moneyball$RS - moneyball$RA
str(moneyball)
# Scatterplot to check for linear relationship
plot(moneyball$RD, moneyball$W)
# Regression model to predict wins
WinsReg = lm(W ~ RD, data=moneyball)
summary(WinsReg)
# Correlation
cor(moneyball$BA,moneyball$OBP)
# Regression model to predict runs scored
RunsReg = lm(RS ~ OBP + SLG + BA, data=moneyball)
summary(RunsReg)
# Regression model to predict runs scored again but removing the batting average
RunsReg = lm(RS ~ OBP + SLG, data=moneyball)
summary(RunsReg)
# Regression model to predict runs allowed
RunsAllowedReg = lm(RA ~ OOBP + OSLG, data=moneyball)
summary(RunsAllowedReg)

In Class Activity 7 Number of Wins

## If a baseball team scores 763 runs and allows 614 ##runs, how many games do we expect the team to win?
##Using the linear regression model constructed during ##the lecture, enter the number of games we expect the ##team to win:

NumberofWins=80.88+0.106*(763-614)
NumberofWins

A team with with runs difference of 149 is expected to win around 97 games

LS0tCnRpdGxlOiAiUiBOb3RlYm9vayIKb3V0cHV0OiBodG1sX25vdGVib29rCi0tLQoKKipDQVAgNDkzNioqCioqLy8gSW4tY2xhc3MgYWN0aXZpdHkgIzcgOiBQcmVkaWN0aW5nIHRoZSBOdW1iZXIgb2YgV2lucyoqIAoKYGBge3J9CmdldHdkKCkKYGBgCmBgYHtyfQojIFJlYWQgaW4gZGF0YQpiYXNlYmFsbCA9IHJlYWQuY3N2KCJiYXNlYmFsbC5jc3YiKQpzdHIoYmFzZWJhbGwpCmBgYAoKYGBge3J9CiMgU3Vic2V0IHRvIG9ubHkgaW5jbHVkZSBtb25leWJhbGwgeWVhcnMKbW9uZXliYWxsID0gc3Vic2V0KGJhc2ViYWxsLCBZZWFyIDwgMjAwMikKc3RyKG1vbmV5YmFsbCkKYGBgCmBgYHtyfQojIENvbXB1dGUgUnV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