install.packages(“rmarkdown”) install.packages(“ppcor”)
# sets wd to the path on my computer;
setwd("C:\\Users\\hmon1\\Desktop\\10C Homework\\") #this is where you downloaded the HW1.csv file
library("ppcor")
## Warning: package 'ppcor' was built under R version 3.5.3
## Loading required package: MASS
# loads in data for the full population
pop<-read.csv("HW24.csv")
names(pop) <- c("X", "Z", "Y")
# sets the seed for the random number generator
set.seed(48183130) #use your student ID instead of 12345678
# assigns a "random" sample of 10 from the population to 'data'
data<-pop[sample(nrow(pop), 10, replace=FALSE),]
# use this matrix
matrix<-round(cor(data),3)
matrix
## X Z Y
## X 1.000 -0.137 0.784
## Z -0.137 1.000 -0.403
## Y 0.784 -0.403 1.000
# r_yx.z
pcor.test(data$Y, data$X, data$Z)
## estimate p.value statistic n gp Method
## 1 0.8043334 0.008957861 3.581528 10 1 pearson
# r_y(x.z)
spcor.test(data$Y, data$X, data$Z)
## estimate p.value statistic n gp Method
## 1 0.7361596 0.02372719 2.877766 10 1 pearson
# r_yz.x
pcor.test(data$Y, data$Z, data$X)
## estimate p.value statistic n gp Method
## 1 -0.4807126 0.1902264 -1.450425 10 1 pearson
# r_y(z.x)
spcor.test(data$Y, data$Z, data$X)
## estimate p.value statistic n gp Method
## 1 -0.2981253 0.4358669 -0.8263419 10 1 pearson
# regression
model <- lm(Y ~ X + Z, data=data)
summary(model)
##
## Call:
## lm(formula = Y ~ X + Z, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.63393 -0.32462 -0.05817 0.53851 1.53208
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.5272 2.1061 1.675 0.13790
## X 0.5830 0.1628 3.582 0.00896 **
## Z -0.2563 0.1767 -1.450 0.19023
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9728 on 7 degrees of freedom
## Multiple R-squared: 0.7043, Adjusted R-squared: 0.6198
## F-statistic: 8.335 on 2 and 7 DF, p-value: 0.01407
# calculates Pearson's r and r2
r2 <-round(summary(lm(Y ~ X + Z, data=data))$r.squared,3)
r2
## [1] 0.704