download.file("http://www.openintro.org/stat/data/mlb11.RData", destfile = "mlb11.RData")
load("mlb11.RData")
##What type of plot would you use to display the relationship between runs and one of the other numerical variables? Plot this relationship using the variable at_bats as the predictor. Does the relationship look linear? If you knew a team’s at_bats, would you be comfortable using a linear model to predict the number of runs? I would use a scatter plot as I did below. The relationship looks lsightly linear, but not enough to be comfortable using a linear regression and model to predict runs very accurately from at bats ( maybe within a 25% ME).
plot(mlb11$at_bats, mlb11$runs)
##Exercise 2: Looking at your plot from the previous exercise, describe the relationship between these two variables. Make sure to discuss the form, direction, and strength of the relationship as well as any unusual observations. There is a positive correlation between ‘at bats’ and ‘runs’. The form is somewhat linear. The strength of the association is moderate. I can identify at least one outlier (from sight; ***not sure the stats to do this.)
plot_ss(x = mlb11$at_bats, y = mlb11$runs)
## Click two points to make a line.
## Call:
## lm(formula = y ~ x, data = pts)
##
## Coefficients:
## (Intercept) x
## -2789.2429 0.6305
##
## Sum of Squares: 123721.9
plot_ss(x = mlb11$at_bats, y = mlb11$runs, showSquares = TRUE)
## Click two points to make a line.
## Call:
## lm(formula = y ~ x, data = pts)
##
## Coefficients:
## (Intercept) x
## -2789.2429 0.6305
##
## Sum of Squares: 123721.9
##Exercise 3:Using plot_ss, choose a line that does a good job of minimizing the sum of squares. Run the function several times. What was the smallest sum of squares that you got? How does it compare to your neighbors? Smallest sum of squares = 123721.9 y^=0.6305*(at_bats)-2789.2429 Multiple R squared = 0.3729 or 37.29% of varibilty in runs is explained by at-bats.
m1 <- lm(runs ~ at_bats, data = mlb11)
summary(m1)
##
## Call:
## lm(formula = runs ~ at_bats, data = mlb11)
##
## Residuals:
## Min 1Q Median 3Q Max
## -125.58 -47.05 -16.59 54.40 176.87
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2789.2429 853.6957 -3.267 0.002871 **
## at_bats 0.6305 0.1545 4.080 0.000339 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 66.47 on 28 degrees of freedom
## Multiple R-squared: 0.3729, Adjusted R-squared: 0.3505
## F-statistic: 16.65 on 1 and 28 DF, p-value: 0.0003388
##Exercise 4: Fit a new model that uses homeruns to predict runs. Using the estimates from the R output, write the equation of the regression line. What does the slope tell us in the context of the relationship between success of a team and its home runs? y= 1.8345*(homerun) + 415.2389 The slope tells us that for every homerun, a team gets 1.8345 runs, or that on average a team has .8345 runners on base when a player hits a homerun (1 run for hitter, .8345 runs for players on base). The slope of the line doesn’t tell us anything about the success of the team unless the sucess of the team is dependent on the runs it scores. The success of the team is also confounded by the “runs scored aginst” them, but if success was defined as runs scored by a team then there is a positive correlation between HR’s and success.
plot_ss(mlb11$homeruns, mlb11$runs)
## Click two points to make a line.
## Call:
## lm(formula = y ~ x, data = pts)
##
## Coefficients:
## (Intercept) x
## 415.239 1.835
##
## Sum of Squares: 73671.99
m2<-lm(runs~homeruns, data = mlb11)
summary(m2)
##
## Call:
## lm(formula = runs ~ homeruns, data = mlb11)
##
## Residuals:
## Min 1Q Median 3Q Max
## -91.615 -33.410 3.231 24.292 104.631
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 415.2389 41.6779 9.963 1.04e-10 ***
## homeruns 1.8345 0.2677 6.854 1.90e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 51.29 on 28 degrees of freedom
## Multiple R-squared: 0.6266, Adjusted R-squared: 0.6132
## F-statistic: 46.98 on 1 and 28 DF, p-value: 1.9e-07
##Exercise 5: If a team manager saw the least squares regression line and not the actual data, how many runs would he or she predict for a team with 5,578 at-bats? Is this an overestimate or an underestimate, and by how much? In other words, what is the residual for this prediction? y^=0.6305*(at_bats)-2789.2429
If a team had 5,578 at bats, a linear regression predicts 727.6861 or aproximately 728 runs.
18 residuals below 0, and 12 above 0. You might argue that this is a slight overestimation because the median residual is negative, meaning for more than half of the teams the linear regression equation is an overestimation.(median residual -16.59)
plot(mlb11$runs ~ mlb11$at_bats)
abline(m1)
atbats<-5578
y_hat<-0.6305*(atbats)-2789.2429;y_hat
## [1] 727.6861
summary(m1)
##
## Call:
## lm(formula = runs ~ at_bats, data = mlb11)
##
## Residuals:
## Min 1Q Median 3Q Max
## -125.58 -47.05 -16.59 54.40 176.87
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2789.2429 853.6957 -3.267 0.002871 **
## at_bats 0.6305 0.1545 4.080 0.000339 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 66.47 on 28 degrees of freedom
## Multiple R-squared: 0.3729, Adjusted R-squared: 0.3505
## F-statistic: 16.65 on 1 and 28 DF, p-value: 0.0003388
mean(m1$residuals) ## basically zero which makes sense
## [1] -1.067202e-15
plot(m1$residuals ~ mlb11$at_bats)
abline(h = 0, lty = 3) # adds a horizontal dashed line at y = 0
##Exercise 6: Is there any apparent pattern in the residuals plot? What does this indicate about the linearity of the relationship between runs and at-bats? There 18 residuals below 0, and 12 above 0, this means that the residuals above 0 have a greater differential. The vast majority of the residuals, don’t mirror eachother on each side of the y = 0 line which suggests a lack in the linear nature to the data. This goes along with the multiple R squared value for this relationship.
hist(m1$residuals)
qqnorm(m1$residuals)
qqline(m1$residuals) # adds diagonal line to the normal prob plot
##Exercise 7:Based on the histogram and the normal probability plot, does the nearly normal residuals condition appear to be met? The normal probability plot suggest a stair stepped and skewed distribution as does the histogram.
##Exercise 8: Based on the plot in (1), does the constant variability condition appear to be met? The constant variability condition says that variability of points around the least squares line should be roughly constant. This implies that the variability of residuals around the zero line should be roughly constant as well. This condition is also called homoscedasticity.
Originally with the naked eye, I would have argued that the variability around the least squares line appeared constant, but the variability of the residuals around the zero line leads me to believe this variability is slightly skewed.
##1. Choose another traditional variable from mlb11 that you think might be a good predictor of runs. Produce a scatterplot of the two variables and fit a linear model. At a glance, does there seem to be a linear relationship? Yes, this is a very linear relationship. I thought they said choose what you predict to be the best variable so I went with new_obs from the start because it basically uses total bases and men on base to predict runs.
plot(mlb11$new_obs, mlb11$runs, xlim = c(.63,.85))
m3<-lm(runs~new_obs, data = mlb11)
abline(m3)
summary(m3)
##
## Call:
## lm(formula = runs ~ new_obs, data = mlb11)
##
## Residuals:
## Min 1Q Median 3Q Max
## -43.456 -13.690 1.165 13.935 41.156
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -686.61 68.93 -9.962 1.05e-10 ***
## new_obs 1919.36 95.70 20.057 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 21.41 on 28 degrees of freedom
## Multiple R-squared: 0.9349, Adjusted R-squared: 0.9326
## F-statistic: 402.3 on 1 and 28 DF, p-value: < 2.2e-16
##2. How does this relationship compare to the relationship between runs and at_bats? Use the R2values from the two model summaries to compare. Does your variable seem to predict runs better than at_bats? How can you tell? This variable is a better predictor of runs than both at_bats, and homeruns, with a multiple R squared value of 0.9349, meaning 93.49% of the varibilty in runs is explained by new_obs.
plot(m3$residuals ~ mlb11$new_obs)
##3. Now that you can summarize the linear relationship between two variables, investigate the relationships between runs and each of the other five traditional variables. Which variable best predicts runs? Support your conclusion using the graphical and numerical methods we’ve discussed (for the sake of conciseness, only include output for the best variable, not all five).
63.48% of the variability in runs can be attributed to batting average based on the multiple R squared. yhat = 5242.2*(bat_avg)
#plot(mlb11$at_bats, mlb11$runs)
#m1<-lm(runs~at_bats, data = mlb11)
#abline(m1)
#summary(m1)
#plot(mlb11$hits, mlb11$runs)
#m1<-lm(runs~hits, data = mlb11)
#abline(m1)
#summary(m1)
#plot(mlb11$homeruns, mlb11$runs)
#m1<-lm(runs~homeruns, data = mlb11)
#abline(m1)
#summary(m1)
plot(mlb11$bat_avg, mlb11$runs)
m1<-lm(runs~bat_avg, data = mlb11)
abline(m1)
summary(m1)
##
## Call:
## lm(formula = runs ~ bat_avg, data = mlb11)
##
## Residuals:
## Min 1Q Median 3Q Max
## -94.676 -26.303 -5.496 28.482 131.113
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -642.8 183.1 -3.511 0.00153 **
## bat_avg 5242.2 717.3 7.308 5.88e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 49.23 on 28 degrees of freedom
## Multiple R-squared: 0.6561, Adjusted R-squared: 0.6438
## F-statistic: 53.41 on 1 and 28 DF, p-value: 5.877e-08
#plot(mlb11$strikeouts, mlb11$runs)
#m1<-lm(runs~strikeouts, data = mlb11)
#abline(m1)
#summary(m1)
#plot(mlb11$stolen_bases, mlb11$runs)
#m1<-lm(runs~stolen_bases, data = mlb11)
#abline(m1)
#summary(m1)
#plot(mlb11$wins, mlb11$runs)
#m1<-lm(runs~wins, data = mlb11)
#abline(m1)
#summary(m1)
##Now examine the three newer variables. These are the statistics used by the author of Moneyball to predict a teams success. In general, are they more or less effective at predicting runs that the old variables? Explain using appropriate graphical and numerical evidence. Of all ten variables we’ve analyzed, which seems to be the best predictor of runs? Using the limited (or not so limited) information you know about these baseball statistics, does your result make sense?
These three predictors, new_onbase, new_slug, and new_obs discussed as my variable in number one are all according to linear regression, better predictors of runs than the old variables. Of all the tenvariables analyzed, new_obs seems to be the best predictor of runs. This makes sense in that it seems to take into account runners on base, batting avg, and the total bases that a player get all in one variable
plot(mlb11$new_onbase, mlb11$runs)
m1<-lm(runs~new_onbase, data = mlb11)
abline(m1)
summary(m1)
##
## Call:
## lm(formula = runs ~ new_onbase, data = mlb11)
##
## Residuals:
## Min 1Q Median 3Q Max
## -58.270 -18.335 3.249 19.520 69.002
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1118.4 144.5 -7.741 1.97e-08 ***
## new_onbase 5654.3 450.5 12.552 5.12e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 32.61 on 28 degrees of freedom
## Multiple R-squared: 0.8491, Adjusted R-squared: 0.8437
## F-statistic: 157.6 on 1 and 28 DF, p-value: 5.116e-13
plot(mlb11$new_slug, mlb11$runs)
m1<-lm(runs~new_slug, data = mlb11)
abline(m1)
summary(m1)
##
## Call:
## lm(formula = runs ~ new_slug, data = mlb11)
##
## Residuals:
## Min 1Q Median 3Q Max
## -45.41 -18.66 -0.91 16.29 52.29
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -375.80 68.71 -5.47 7.70e-06 ***
## new_slug 2681.33 171.83 15.61 2.42e-15 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 26.96 on 28 degrees of freedom
## Multiple R-squared: 0.8969, Adjusted R-squared: 0.8932
## F-statistic: 243.5 on 1 and 28 DF, p-value: 2.42e-15
#5 Check the model diagnostics for the regression model with the variable you decided was the best predictor for runs. The residuals looks relatively normal and pretty tight around the qqline, not as close as I would have thought that they would be based on the Multiple R squared number.
hist(m3$residuals)
qqnorm(m3$residuals)
qqline(m3$residuals)