-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment4.Rmd
More file actions
222 lines (145 loc) · 7.87 KB
/
assignment4.Rmd
File metadata and controls
222 lines (145 loc) · 7.87 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
---
title: "Principal Component Aanalysis"
output: html_document
---
#Data
The data you will be using comes from teh Assistments online intelligent tutoring system (https://www.assistments.org/). It describes students working through online math problems. Each student has the following data associated with them:
- id
- prior_prob_count: How many problems a student has answered in the system prior to this session
- prior_percent_correct: The percentage of problems a student has answered correctly prior to this session
- problems_attempted: The number of problems the student has attempted in the current session
- mean_correct: The average number of correct answers a student made on their first attempt at problems in the current session
- mean_hint: The average number of hints a student asked for in the current session
- mean_attempt: The average number of attempts a student took to answer a problem in the current session
- mean_confidence: The average confidence each student has in their ability to answer the problems in the current session
#Start by uploading the data
```{r}
D1<-read.table("Assistments-confidence.csv", sep = ",", header = TRUE)
#We won't need to id variable, so remove that.
D2<-D1
D2$id<-NULL
```
#Create a correlation matrix of the relationships between the variables, including correlation coefficients for each pair of variables/features.
```{r}
#You can install the corrplot package to plot some pretty correlation matrices (sometimes called correlograms)
library(corrplot)
#Generate pairwise correlations
COR <- cor(D2)
corrplot(COR, order="AOE", method="circle", tl.pos="lt", type="upper",
tl.col="black", tl.cex=0.6, tl.srt=45,
addCoef.col="black", addCoefasPercent = TRUE,
sig.level=0.50, insig = "blank")
#Study your correlogram image and save it, you will need it later
```
#Create a new data frame with the mean_correct variables removed
```{r}
D3<-D2
D3$mean_correct<-NULL
#The, scale and center your data for easier interpretation
D4 <- scale(D3, center = TRUE)
```
#Now run the PCA on the new data frame
```{r}
pca <- prcomp(D4, scale = TRUE)
```
#Although the algorithm does not generate the eigenvalues directly for us, we can print a list of the standard deviation of the variance accounted for by each component.
```{r}
pca$sdev
#To convert this into variance accounted for we can square it, these numbers are proportional to the eigenvalue
pca$sdev^2
#A summary of our pca will give us the proportion of variance accounted for by each component
summary(pca)
#We can lot this to get an idea of which components we should keep and which we should drop
plot(pca, type = "lines")
```
#Think about which components you would drop and make a decision
Based on the plot we should drop PC^ beause it is very close to zero, thus having a low variance.
```{r}
#Now, create a data frame of the transformed data from your pca.
D5 <- as.data.frame(pca$x)
#Attach the variable "mean_correct" from your original data frame to D5.
D6 <- cbind(D5, as.data.frame(D1$mean_correct))
#Now re-run your scatterplots and correlations between the transformed data and mean_correct. If you had dropped some components would you have lost important infomation about mean_correct?
#Yes. The components give you context into what is happening. So, if you had dropped some componenents you would have lost some information (even if it was minimal).
COR2 <- cor(D6)
corrplot(COR2, order="AOE", method="circle", tl.pos="lt", type="upper",
tl.col="black", tl.cex=0.6, tl.srt=45,
addCoef.col="black", addCoefasPercent = TRUE,
sig.level=0.50, insig = "blank")
```
#Now print out the eigenvectors (often called loadings) for the components you generated:
```{r}
pca$rotation
#Examine the eigenvectors, notice that they are a little difficult to interpret. It is much easier to make sense of them if we make them proportional within each component
loadings <- abs(pca$rotation) #abs() will make all eigenvectors positive
sweep(loadings, 2, colSums(loadings), "/") #sweep() computes each row as a proportion of the column. (There must be a way to do this with dplyr()?)
#Make a table for eigenvectors to make the computations simpler to input
A1<-sweep(loadings, 2, colSums(loadings), "/")
eigenvectors<-t(A1)
#Now examine your components and try to come up with substantive descriptions of what some might represent?
PC1 might be persistence
PC3 might be confidence
PC5 mighte be resilience
#How I came up with the descriptions above. Qualatative analysis based on eigenvectors values, which is then based on categorical comparison (how one categories' eigenvalue relates to another).
#You can generate a biplot to help you, though these can be a bit confusing. They plot the transformed data by the first two components. Therefore, the axes represent the direction of maximum variance. Then mapped onto this point cloud are the original directions of the variables, depicted as red arrows. It is supposed to provide a visualization of which variables "go together". Variables that possibly represent the same underlying construct point in the same direction.
biplot(pca)
#Calculate values for each student that represent these your composite variables and then create a new correlogram showing their relationship to mean_correct.
#Duplicate data, just in case you (I) mess up. I tried using the scaled data, but it never worked. So I ended up using the dataset that I did not scale. This is probably wrong, but this is the only way that worked.
E1<-D3
E1$persist<-(eigenvectors[1,1]*E1[,1])+(eigenvectors[1,2]*E1[,2])+(eigenvectors[1,3]*E1[,3]) +(eigenvectors[1,4]*E1[,4])+(eigenvectors[1,5]*E1[,5])+(eigenvectors[1,6]*E1[,6])
E1$confidence<-(eigenvectors[3,1]*E1[,1])+(eigenvectors[3,2]*E1[,2])+(eigenvectors[3,3]*E1[,3]) +(eigenvectors[3,4]*E1[,4])+(eigenvectors[3,5]*E1[,5])+(eigenvectors[3,6]*E1[,6])
E1$resilience<-(eigenvectors[5,1]*E1[,1])+(eigenvectors[5,2]*E1[,2])+(eigenvectors[5,3]*E1[,3]) +(eigenvectors[5,4]*E1[,4])+(eigenvectors[5,5]*E1[,5])+(eigenvectors[5,6]*E1[,6])
COR3<-cor(E1)
corrplot(COR3, order="AOE", method="circle", tl.pos="lt", type="upper",
tl.col="black", tl.cex=0.6, tl.srt=45,
addCoef.col="black", addCoefasPercent = TRUE,
sig.level=0.50, insig = "blank")
```
#Also in this repository is a data set and codebook from Rod Martin, Patricia Puhlik-Doris, Gwen Larsen, Jeanette Gray, Kelly Weir at the University of Western Ontario about people's sense of humor. Can you perform a PCA on this data?
```{r}
#Upload the data into a table
Z1<-read.table("humor_data.csv", sep = ",", header = TRUE)
#Method 1
#make new data table (eliminating variables at end)
Z2<Z1
Z2<-dplyr::select(Z2,Q1:Q32)
#generate pairwaise correlations
COR4<-cor(Z2)
corrplot(COR4, order="AOE", method="circle", tl.pos="lt", type="upper",
tl.col="black", tl.cex=0.6, tl.srt=45,
addCoef.col="black", addCoefasPercent = TRUE,
sig.level=0.50, insig = "blank")
#Run PCA on new data frame
pca2 <- prcomp(Z2, scale = TRUE)
pca2$sdev
pca2$sdev^2
summary(pca2)
plot(pca2, type = "lines")
#Maybe we should drop 9 & 10 as they are near 0 variance.
pca2$rotation
loadings2<-abs(pca2$rotation)
A2<-sweep(loadings2, 2, colSums(loadings2), "/")
eigenvectors2<-t(A2)
#Method 2
#Make new data table (Utilizing all variables)
ZZ<-Z1
#generate pairwaise correlations
COR5<-cor(ZZ)
corrplot(COR5, order="AOE", method="circle", tl.pos="lt", type="upper",
tl.col="black", tl.cex=0.6, tl.srt=45,
addCoef.col="black", addCoefasPercent = TRUE,
sig.level=0.50, insig = "blank")
#Run PCA on new data frame
pca3 <- prcomp(ZZ, scale = TRUE)
pca3$sdev
pca3$sdev^2
summary(pca3)
plot(pca3, type = "lines")
#We should not drop any because they are all either above one or very close to 1 (Variance wise).
pca3$rotation
loadings3<-abs(pca3$rotation)
A3<-sweep(loadings3, 2, colSums(loadings3), "/")
eigenvectors3<-t(A3)
biplot(pca2)
biplot(pca3)
```