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("HW23.csv")
names(pop) <- c("X1", "X2", "Y")
# sets the seed for the random number generator
set.seed(48183130) #use your student ID instead of 12345678
# assigns a "random" sample of 29 from the population to 'data'
data<-pop[sample(nrow(pop), 29, replace=FALSE),]
# use this matrix
matrix<-round(cor(data),3)
matrix
## X1 X2 Y
## X1 1.000 0.384 0.539
## X2 0.384 1.000 0.172
## Y 0.539 0.172 1.000
# multiple regression model
model <- lm(Y ~ X1 + X2, data=data)
summary(model)
##
## Call:
## lm(formula = Y ~ X1 + X2, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.7796 -0.9972 -0.1482 1.0951 2.1281
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.47697 1.08242 3.212 0.00350 **
## X1 0.37509 0.12076 3.106 0.00454 **
## X2 -0.03296 0.14312 -0.230 0.81968
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.276 on 26 degrees of freedom
## Multiple R-squared: 0.2923, Adjusted R-squared: 0.2379
## F-statistic: 5.369 on 2 and 26 DF, p-value: 0.01117
# standardized beta coefficients
model_beta <- lm(scale(Y) ~ scale(X1) + scale(X2), data=data)
summary(model_beta)
##
## Call:
## lm(formula = scale(Y) ~ scale(X1) + scale(X2), data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.2178 -0.6824 -0.1014 0.7494 1.4563
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.934e-16 1.621e-01 0.000 1.00000
## scale(X1) 5.551e-01 1.787e-01 3.106 0.00454 **
## scale(X2) -4.115e-02 1.787e-01 -0.230 0.81968
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.873 on 26 degrees of freedom
## Multiple R-squared: 0.2923, Adjusted R-squared: 0.2379
## F-statistic: 5.369 on 2 and 26 DF, p-value: 0.01117
# calculates Pearson's r and r2
r2 <-round(summary(model)$r.squared,3)
r <-round(sqrt(r2),3)
r
## [1] 0.54
r2
## [1] 0.292