Markdown Author: Jessie Bell, 2023

Libraries Used: ggplot2

Part 1: Regression

1

Include the excel graph in your write-up.

2

predictor <- c(4.2,5.6,5.1,4.6,9.2,6.9,10.0,3.4,7.0,11.2,1.2,8.7)
response <- c(6.9,8.3,7.3,6.3,11.2,8.1,13.6,5.7,8.0,13.8,3.3,12.8)
 
data <- data.frame(predictor, response)
fit <- lm(response ~ predictor, data = data)

3

\(H_0\): The slope of the regression line is not significantly different from zero.

\(H_a\): The slope of the regression line is significantly different from zero.

4

fit <- lm(response ~ predictor, data = data)
anova(fit)
## Analysis of Variance Table
## 
## Response: response
##           Df  Sum Sq Mean Sq F value    Pr(>F)    
## predictor  1 115.298 115.298  146.98 2.653e-07 ***
## Residuals 10   7.844   0.784                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(fit)
## 
## Call:
## lm(formula = response ~ predictor, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.4045 -0.5110  0.1064  0.4615  1.5342 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.74045    0.63407   2.745   0.0207 *  
## predictor    1.09487    0.09031  12.124 2.65e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8857 on 10 degrees of freedom
## Multiple R-squared:  0.9363, Adjusted R-squared:  0.9299 
## F-statistic:   147 on 1 and 10 DF,  p-value: 2.653e-07

5

ggplot(data, aes(predictor, response))+
  geom_point(color="#ed68ed")+
   geom_smooth(formula = y ~ x, method = "lm", color="#00c19a")+
  labs(title="Response vs. Predictor")

6

plot(fit$fitted.values, fit$residuals, xlab = "Predicted scores", ylab = "Residuals")|>
abline(0, 0)

Part 2: Correlations

7

correlation <- cor(response, predictor, method = "pearson")

8

\(H_0\): There is no correlation between the variables.

\(H_a\): There is a significant correlation between the variables.

9

cor.test(response, predictor)
## 
##  Pearson's product-moment correlation
## 
## data:  response and predictor
## t = 12.124, df = 10, p-value = 2.653e-07
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.8854130 0.9911306
## sample estimates:
##       cor 
## 0.9676255

10

cor.test(response, predictor, alternative = "less")
## 
##  Pearson's product-moment correlation
## 
## data:  response and predictor
## t = 12.124, df = 10, p-value = 1
## alternative hypothesis: true correlation is less than 0
## 95 percent confidence interval:
##  -1.0000000  0.9890686
## sample estimates:
##       cor 
## 0.9676255

11

Changing the method from “pearson” to “spearman” may alter the test results. Discuss how this affects the interpretation.

Part 3

12

Include the plot with additional variables in your write-up.

#Create some more fake data 
data2 <- data.frame(data, Predictor2 = rpois(12, 3), Response2 = sort(rnorm(12, 23)), CatPredictor = gl(3, 4, labels = c("Low", "Med", "High")))
#Plot the all the fake data
plot(data2)

13

data.frame(data, Predictor2 = rpois(12, 3), Response2 = sort(rnorm(12, 23)), CatPredictor = gl(3, 4, labels = c("Low", "Med", "High")))
##    predictor response Predictor2 Response2 CatPredictor
## 1        4.2      6.9          3  21.49213          Low
## 2        5.6      8.3          5  21.94792          Low
## 3        5.1      7.3          1  23.14765          Low
## 4        4.6      6.3          1  23.25382          Low
## 5        9.2     11.2          3  23.34147          Med
## 6        6.9      8.1          3  23.45784          Med
## 7       10.0     13.6          4  23.57566          Med
## 8        3.4      5.7          1  23.72436          Med
## 9        7.0      8.0          0  23.84807         High
## 10      11.2     13.8          4  23.98014         High
## 11       1.2      3.3          2  23.99908         High
## 12       8.7     12.8          4  24.91393         High
#Plot the all the fake data
plot(data2)

14

corInfo <- cor.test(data$predictor, data$response)

plot(response ~ predictor, data = data, pch = 16)
text(2, 13, paste("r =", signif(corInfo[["estimate"]], 3), "\nP =", signif(corInfo[["p.value"]], 3)), pos = 4)

abline(fit, col = "grey")
#I pull information out of fit to display on the graph
i=round(fit$coef[[1]], 2)
s=round(fit$coef[[2]], 2)
text(2, 13, paste("y=", i, "+", s, "*x"), pos = 4)