-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSY5013_HW3.R
122 lines (92 loc) · 3.72 KB
/
PSY5013_HW3.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
rm(list = ls(all.names = TRUE))
library(foreign)
#Reading in Data
ds<-read.table("health.dat")
colnames(ds)<-c("case", "age", "income", "havephys", "physrec", "womrel", "lump", "suscept1", "severe1", "benefit1", "barrier1", "intent1", "comply")
#Question1
#Reformatting table into that form from Slideshow 11, slide 14
Model1<-lm(intent1~havephys+physrec,data=ds)
Model1
Model2<-lm(intent1~havephys+physrec+lump+womrel, data=ds)
Model2
Model3<-lm(intent1~havephys+physrec+lump+womrel+suscept1+severe1+benefit1+barrier1, data=ds)
Model3
Model1Coef <-round(summary(Model1)$coefficients[,1], digits=3)
Model1Coef <-c(Model1Coef, ".",".",".",".",".",".")
Model1Se <-round(summary(Model1)$coefficients[,2], digits=3)
Model1Se <-c(Model1Se, ".",".",".",".",".",".")
Model1subtable<-cbind(Model1Coef, Model1Se)
Model2Coef <-round(summary(Model2)$coefficients[,1], digits=3)
Model2Coef <-c(Model2Coef, ".",".",".",".")
Model2Se <-round(summary(Model2)$coefficients[,2], digits=3)
Model2Se <-c(Model2Se, ".",".",".",".")
Model2subtable<-cbind(Model2Coef, Model2Se)
Model3Coef <-round(summary(Model3)$coefficients[,1], digits=3)
Model3Se <-round(summary(Model3)$coefficients[,2], digits=3)
Model3subtable<-cbind(Model3Coef, Model3Se)
hrtable<-cbind(Model1subtable, Model2subtable, Model3subtable)
hrtable<-data.frame(hrtable)
hrtable1<-hrtable[2:9,]
hrtable1<-data.frame(hrtable1, row.names=c("havephys", "physrec", "lump", "womrel", "suscept1", "severe1", "benefit1", "barrier1"))
yhavephys <-cor(x=ds$havephys, y=ds$intent1)
yphysrec <-cor(x=ds$physrec, y=ds$intent1)
ylump <-cor(x=ds$lump, y=ds$intent1)
ywomrel <-cor(x=ds$womrel, y=ds$intent1)
ysuscept <-cor(x=ds$suscept1, y=ds$intent1)
ysevere <-cor(x=ds$severe1, y=ds$intent1)
ybenefit <-cor(x=ds$benefit1, y=ds$intent1)
ybarrier <-cor(x=ds$barrier, y=ds$intent1)
cor.with.y<-round(as.vector(c(yhavephys, yphysrec, ylump, ywomrel, ysuscept, ysevere, ybenefit, ybarrier)), digits=3)
Q1table<-cbind(cor.with.y, hrtable1)
Q1table
#Question 2
summary(Model1)
anova(Model1, Model2, Model3)
#Question 3
dsGPA<-read.table("HW3_GPA.dat")
colnames(dsGPA)<-c("CGPA", "HGPA", "SAT", "SEX")
#Question 3-1
#A
#Semipartial correlation between CGPA and SAT by partialling effect of HGPA from CGPA.
#Regress CGPA onto HGPA; compute residuals; find correlation between SAT and residuals of CGPA
CregH<-lm(CGPA~HGPA, data=dsGPA)
CregHresiduals<-as.vector(CregH$residuals)
dsGPA<-cbind(dsGPA, CregHresiduals)
SATcorCresid<-cor(dsGPA$CregHresiduals, dsGPA$SAT)
SATcorCresid
#B
#Semipartial of CGPA and SAT, controlling for HGPA, using correlation coefficients
ScorC<-cor(dsGPA$SAT, dsGPA$CGPA)
ScorH<-cor(dsGPA$SAT, dsGPA$HGPA)
CcorH<-cor(dsGPA$CGPA, dsGPA$HGPA)
ScorC
ScorH
CcorH
#C
#Semipartial of CGPA and SAT, removing effects of HGPA on CGPA, using squared multiple correlation coefficients.
ModelSATonCH<-lm(SAT~CGPA+HGPA, data=dsGPA)
summary(ModelSATonCH)
ModelSATonH<-lm(SAT~HGPA, data=dsGPA)
summary(ModelSATonH)
#Question 3-2
#A
#Computing the correlation of the residuals of SAT regressed onto HGPA and the residuals of CGPA regressed onto HGPA.
SregH<-lm(SAT~HGPA, data=dsGPA)
SregHresiduals<-as.vector(SregH$residuals)
dsGPA<-cbind(dsGPA, SregHresiduals)
SresidCorCresid<-cor(dsGPA$SregHresiduals, dsGPA$CregHresiduals)
SresdiCorCresid
#B
#Semipartial of CGPA and SAT, controlling for HGPA, using correlation coefficients
ScorC<-cor(dsGPA$SAT, dsGPA$CGPA)
ScorH<-cor(dsGPA$SAT, dsGPA$HGPA)
CcorH<-cor(dsGPA$CGPA, dsGPA$HGPA)
ScorC
ScorH
CcorH
#C
#Semipartial of CGPA and SAT, removing effects of HGPA on CGPA, using squared multiple correlation coefficients.
ModelSATonCH<-lm(SAT~CGPA+HGPA, data=dsGPA)
summary(ModelSATonCH)
ModelSATonH<-lm(SAT~HGPA, data=dsGPA)
summary(ModelSATonH)