install.packages(“rmarkdown”)
# sets wd to the path on my computer;
setwd("C:\\Users\\hmon1\\Desktop\\10C Homework\\") #this is where you downloaded the HW1.csv file
# loads in data for the full population
pop<-read.csv("HW19.csv", head = TRUE)
names(pop) <- c("X", "Y")
# sets the seed for the random number generator
set.seed(48183130) #use your student ID instead of 12345678
# assigns a "random" sample of 12 from the population to 'data'
data<-pop[sample(nrow(pop), 12, replace=FALSE),]
# use this data
data
## X Y
## 640 5 5
## 413 12 8
## 871 9 6
## 336 5 5
## 357 7 6
## 238 7 6
## 56 5 5
## 83 8 6
## 351 7 6
## 632 9 6
## 477 9 6
## 458 6 5
# regression
model <- lm(Y ~ X, data=data)
summary(model)
##
## Call:
## lm(formula = Y ~ X, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.4106 -0.3403 0.0477 0.3186 0.4957
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.12947 0.37516 8.342 8.15e-06 ***
## X 0.36457 0.04881 7.470 2.14e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.3414 on 10 degrees of freedom
## Multiple R-squared: 0.848, Adjusted R-squared: 0.8328
## F-statistic: 55.79 on 1 and 10 DF, p-value: 2.136e-05
# calculates Pearson's r, r2, and obt
r <- round(cor(data$X, data$Y),3)
r2 <- round(r^2,3)
n <- length(data$X)
obt <- r*(sqrt((n-2)/(1-r2)))
r
## [1] 0.921
r2
## [1] 0.848
obt
## [1] 7.470296
# creates plot
plot(data$X, data$Y, main=c(paste("Scatterplot")), xlim=c(0,10), ylim=c(0,10), xlab="X", ylab="Y")
abline(lm(Y ~ X, data=data))