Exercise 1: 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?

In this case I would use a scatter point ass it would allow for the two numerical data points to be examined, along with other relations such as the run/

download.file("http://www.openintro.org/stat/data/mlb11.RData", destfile = "mlb11.RData")
load("mlb11.RData")
plot(mlb11$runs~mlb11$at_bats,
     main = "Relations",
     col = "sky blue",
     pch=19,)

cor(mlb11$runs, mlb11$at_bats)
## [1] 0.610627

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.

From observation/glance their doesn’t seem to be a strong correlation, due to factors such as the notable postie outlires. We can also use a code to give us a correlation, the closer it is to one the stronger the correlation, which even then, seems to imply that theirs is a very weak correlation of 0.6101, which is very far from (positive)1. Overall the relationship dose seem to be positive as the runs increase based on the bats, buts it seem to end their.

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? After doing it well above 10 times, the closet I got to the plot_ss sum of squares was 130685.4, which although not that far, is likely far in comparison to what my peers got.

Call: lm(formula = y ~ x, data = pts)

Coefficients: (Intercept) x
-3791.9524 0.8111

Sum of Squares: 130685.4

###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=415.2389 + 1.8345*homeruns, The slope tell us that based on every home runs team is able to achieve the total average numbers of runs will increase in response, roughly by 1.8345.

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
plot_ss(x=mlb11$runs, y=mlb11$at_bats, showSquares = TRUE)

## Click two points to make a line.
                                
## Call:
## lm(formula = y ~ x, data = pts)
## 
## Coefficients:
## (Intercept)            x  
##   5113.3510       0.5913  
## 
## Sum of Squares:  116027.1
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
plot_ss(x=mlb11$homeruns, y=mlb11$runs, showSquares = TRUE)

## 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

###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? If we substitute the formulas we would estimate that roughly 730 runs would be assumed to occur if 5,578 at bats occurred. Based on the date their is no exact team that did had these bats, but the Philly do come close, with 5579 and 713 runs. Subsisting these, we can see we overestimated by roughly 17

plot(mlb11$runs ~ mlb11$at_bats)

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

## 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
a0 <- -2789.243
a1 <- 0.631
x <- 5578
yh <- a0 + a1*x
730-713
## [1] 17

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?

The residuals don’t seem to show an obvious patterns and seem to be scattered at random. With no pattern this wold make it seem that it contains a linear regression. Likely being a nearly normal residual plot.

plot(m1$residuals ~ mlb11$at_bats)

hist(m1$residuals,
     col = "Lavender")

qqnorm(m1$residuals,
       col = "sky blue", 
       pch=19)

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

Although with it being slightly skewed, its and artifacts in the qq, nearly normal residuals due seem to be met, or ar least close enough to be met. ### 8: Based on the plot in (1), does the constant variability condition appear to be met? Yes it dose seem to be met. Left off on the on your won part.

ON YOUR OWN

1.)

I choose hits this time around and at first glance the two variable do seem to fit a linear model

mm <- lm(runs ~ hits, data = mlb11)
plot(mlb11$runs ~ mlb11$hits, main= "realtion")

summary(mm)
## 
## Call:
## lm(formula = runs ~ hits, data = mlb11)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -103.718  -27.179   -5.233   19.322  140.693 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -375.5600   151.1806  -2.484   0.0192 *  
## hits           0.7589     0.1071   7.085 1.04e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 50.23 on 28 degrees of freedom
## Multiple R-squared:  0.6419, Adjusted R-squared:  0.6292 
## F-statistic:  50.2 on 1 and 28 DF,  p-value: 1.043e-07

2.)

Runs and bats had an R2 of 0.3792 compared to runs and hits R2 of 0.6419, turning these in to percents, mine variable is able to find 64.19% of all the variability within the model compares to 37.92% of run and bats. In short yes it dose as the R2, between my variable and the given is able to cover I wider variability.

3.)

The best variable seems to be using bat_avg, as it has the highest R2 value based on the summary.

hist(mm$residuals
     ,col = "Lavender")

qqnorm(mm$residuals
       ,col = "sky blue"
       ,pch=19)

m3 <- lm(runs ~ bat_avg, data=mlb11)
plot(mlb11$runs ~ mlb11$bat_avg)

summary (m3)
## 
## 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
hist(m3$residuals
     ,col = "Lavender")

qqnorm(m3$residuals
       ,col = "sky blue"
       ,pch=19)

4.)

Going purely off the provided summary, the most effective one that would be used to predict run would be new_obs due to its R2 being 0.935 or 93.5%, which is a high indicator

names(mlb11)
##  [1] "team"         "runs"         "at_bats"      "hits"         "homeruns"    
##  [6] "bat_avg"      "strikeouts"   "stolen_bases" "wins"         "new_onbase"  
## [11] "new_slug"     "new_obs"
new_onbas <- lm(runs ~ new_obs, data = mlb11)
new_sl <- lm(runs ~ new_slug, data= mlb11)
new_obs <- lm(runs ~ new_obs, data=mlb11)
summary(new_onbas)
## 
## 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
summary(new_sl)
## 
## 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
summary(new_obs)
## 
## 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