download.file("http://www.openintro.org/stat/data/mlb11.RData", destfile = "mlb11.RData")
load("mlb11.RData")

abc <- "http://www.openintro.org/stat/data/mlb11.RData"

Exercise 1:What type of plot would you use to display the relationship between runs and one of the other numerical variables? I would use a scatter plot.

m <- cor(mlb11$runs, mlb11$at_bats)
m
## [1] 0.610627
plot( mlb11$at_bats, mlb11$runs, xlim=c (5200, 6000))

sum of squared residuals

excercise 2: Looking at your plot from the previous exercise, describe the relationship between these two variables.

it does not appear to be very corrolated, however it does potentially have a slight positive coorolation

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

Excersice 3: Using plot_ss, choose a line that does a good job of minimizing the sum of squares.

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

The smallest I get is 123721.9

the linear model

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

Fit a new model that uses homeruns to predict runs.

plot (mlb11$homeruns, mlb11$runs)

m1 <- lm(homeruns ~ runs, data = mlb11)
summary(m1)
## 
## Call:
## lm(formula = homeruns ~ runs, data = mlb11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -52.067 -15.794   3.702  15.766  39.232 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -85.15663   34.79698  -2.447   0.0209 *  
## runs          0.34154    0.04983   6.854  1.9e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 22.13 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

the slope would tell realtionship between the 2 varalibes

Prediction and prediction errors

plot(mlb11$runs ~ mlb11$at_bats)
abline(m1)

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?

 (0.34154*5578)-85.15663 
## [1] 1819.953

it would be an over estimation

Model diagnostics

plot(m1$residuals ~ mlb11$at_bats)
abline(h = 0, lty = 3)

### Is there any apparent pattern in the residuals plot? What does this indicate about the linearity of the relationship between runs and at-bats?

does not appear to have any patterns

hist(m1$residuals)

qqnorm(m1$residuals)
qqline(m1$residuals)

exercise 7: Based on the histogram and the normal probability plot, does the nearly normal residuals condition appear to be met?

it is skewed left, however with the exection of that it is fairly normal.

excersice 8: Based on the plot in (1), does the constant variability condition appear to be met?

yes the points appear to be randonly distributed

on your own

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? [wins]

plot(mlb11$runs, mlb11$wins)

maybe a slight positive coorolation

2. How does this relationship compare to the relationship between runs and at_bats? Use the R2 values from the two model summaries to compare. Does your variable seem to predict runs better than at_bats? How can you tell?

plot_ss(x = mlb11$wins, y = mlb11$runs)

## Click two points to make a line.
                                
## Call:
## lm(formula = y ~ x, data = pts)
## 
## Coefficients:
## (Intercept)            x  
##     342.121        4.341  
## 
## Sum of Squares:  126068.4

wins sum of squares = 126068.4; bats sum of squares = 123721.9. so bat’s is less, and therefore at bats is a better predictor

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?

plot_ss(x = mlb11$hits, y = mlb11$runs)

## Click two points to make a line.
                                
## Call:
## lm(formula = y ~ x, data = pts)
## 
## Coefficients:
## (Intercept)            x  
##   -375.5600       0.7589  
## 
## Sum of Squares:  70638.75

hits is the best with the sum of suares being 70638.75

4. 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?

plot_ss(x = mlb11$new_obs, y = mlb11$runs)

## Click two points to make a line.
                                
## Call:
## lm(formula = y ~ x, data = pts)
## 
## Coefficients:
## (Intercept)            x  
##      -686.6       1919.4  
## 
## Sum of Squares:  12837.66

yes. they all predict it much better then even the best of the 7 old ones. the most efficent. the best predicter seems to be observations, which with my very limmited understanding of baseball I never would have suspected.

5. Check the model diagnostics for the regression model with the variable you decided was the best predictor for runs.

m2 <- lm(runs ~ at_bats, data = mlb11)
plot(x = mlb11$new_obs, y = mlb11$runs)
abline(m2)

summary(m2)
## 
## 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
plot(m2$residuals ~ mlb11$new_obs)
abline(h = 0, lty = 3)

hist(m2$residuals)

qqnorm(m2$residuals)
qqline(m2$residuals)