diff --git a/assignment5.Rmd b/assignment5.Rmd index 288bcb3..c8299e5 100644 --- a/assignment5.Rmd +++ b/assignment5.Rmd @@ -16,7 +16,7 @@ The data you will be using comes from the Assistments online intelligent tutorin ## Start by uploading the data ```{r} -D1 <- +D1<-read.csv("Assistments-confidence.csv" , header = T) ``` @@ -38,7 +38,7 @@ ggcorr(D1[,-1], method = c("everything", "pearson")) #ggcorr() doesn't have an e ## Create a new data frame with the mean_correct variable removed, we want to keep that variable intact. The other variables will be included in our PCA. ```{r} -D2 <- +D2<-D1[,-5] ``` @@ -67,35 +67,43 @@ plot(pca, type = "lines") ``` ## Decide which components you would drop and remove them from your data set. +```{r} +PCAdata<-data.frame(pca$x) +``` ## Part II + ```{r} #Now, create a data frame of the transformed data from your pca. -D3 <- - -#Attach the variable "mean_correct" from your original data frame to D3. +D3<-PCAdata[,1:5] +#Attach the variable "mean_correct" from your original data frame to D3.Now re-run your correlation plots between the transformed data and mean_correct. If you had dropped some components would you have lost important infomation about mean_correct? +D4<-cbind(D3,D1[5]) +ggpairs(D4, 1:6, progress = FALSE) +ggcorr(D4, method = c("everything", "pearson")) +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 -#Now re-run your correlation plots between the transformed data and mean_correct. If you had dropped some components would you have lost important infomation about mean_correct? +loadings <- abs(pca$rotation) #abs() will make all eigenvectors positive +#Now examine your components and try to come up with substantive descriptions of what some might represent? +#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 accounted for. 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) ``` -## Now print out the 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 -#Now examine your components and try to come up with substantive descriptions of what some might represent? -#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 accounted for. 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) @@ -105,6 +113,17 @@ biplot(pca) Also in this repository is a data set collected from TC students (tc-program-combos.csv) that shows how many students thought that a TC program was related to andother TC program. Students were shown three program names at a time and were asked which two of the three were most similar. Use PCA to look for components that represent related programs. Explain why you think there are relationships between these programs. ```{r} +#T<-read.csv("tc-program-combos.csv") +# library(ggplot2) +# library(GGally) +# ggpairs(T, 2:68, progress = FALSE) +# ggcorr(T[,-1], method = c("everything", "pearson")) +# pca1 <- prcomp(T[-1], scale. = TRUE) +# pca1$sdev +# pca1$sdev^2 +# summary(pca1) +# plot(pca1, type = "lines") + ``` diff --git a/assignment5.html b/assignment5.html new file mode 100644 index 0000000..491e2cb --- /dev/null +++ b/assignment5.html @@ -0,0 +1,556 @@ + + + + + + + + + + + + + +Principle Component Aanalysis + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +
+

Data

+

The data you will be using comes from the 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:

+ +
+
+

Start by uploading the data

+
D1<-read.csv("Assistments-confidence.csv" , header = T)
+
+
+

Create a correlation matrix of the relationships between the variables, including correlation coefficients for each pair of variables/features.

+
#You can install the corrplot package to plot some pretty correlation matrices (sometimes called correlograms)
+
+library(ggplot2)
+library(GGally)
+
## Registered S3 method overwritten by 'GGally':
+##   method from   
+##   +.gg   ggplot2
+
ggpairs(D1, 2:8, progress = FALSE) #ggpairs() draws a correlation plot between all the columns you identify by number (second option, you don't need the first column as it is the student ID) and progress = FALSE stops a progress bar appearing as it renders your plot
+

+
ggcorr(D1[,-1], method = c("everything", "pearson")) #ggcorr() doesn't have an explicit option to choose variables so we need to use matrix notation to drop the id variable. We then need to choose a "method" which determines how to treat missing values (here we choose to keep everything, and then which kind of correlation calculation to use, here we are using Pearson correlation, the other options are "kendall" or "spearman")
+

+
#Study your correlogram images and save them, you will need them later. Take note of what is strongly related to the outcome variable of interest, mean_correct. 
+
+
+

Create a new data frame with the mean_correct variable removed, we want to keep that variable intact. The other variables will be included in our PCA.

+
D2<-D1[,-5]
+
+
+

Now run the PCA on the new data frame

+
pca <- prcomp(D2, scale. = TRUE)
+
+
+

Although princomp 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.

+
pca$sdev
+
## [1] 1.4502917 1.1428589 1.0462819 0.9976299 0.8557032 0.7385185 0.4721430
+
#To convert this into variance accounted for we can square it, these numbers are proportional to the eigenvalue
+
+pca$sdev^2
+
## [1] 2.1033459 1.3061264 1.0947058 0.9952654 0.7322279 0.5454096 0.2229190
+
#A summary of our pca will give us the proportion of variance accounted for by each component
+
+summary(pca)
+
## Importance of components:
+##                           PC1    PC2    PC3    PC4    PC5     PC6     PC7
+## Standard deviation     1.4503 1.1429 1.0463 0.9976 0.8557 0.73852 0.47214
+## Proportion of Variance 0.3005 0.1866 0.1564 0.1422 0.1046 0.07792 0.03185
+## Cumulative Proportion  0.3005 0.4871 0.6434 0.7856 0.8902 0.96815 1.00000
+
#We can look at this to get an idea of which components we should keep and which we should drop
+
+plot(pca, type = "lines")
+

+
+
+

Decide which components you would drop and remove them from your data set.

+
PCAdata<-data.frame(pca$x)
+
+
+

Part II

+
#Now, create a data frame of the transformed data from your pca.
+
+D3<-PCAdata[,1:5]
+
+#Attach the variable "mean_correct" from your original data frame to D3.Now re-run your correlation plots between the transformed data and mean_correct. If you had dropped some components would you have lost important infomation about mean_correct?
+
+D4<-cbind(D3,D1[5])
+ggpairs(D4, 1:6, progress = FALSE)
+

+
ggcorr(D4, method = c("everything", "pearson")) 
+

+
pca$rotation
+
##                               PC1        PC2          PC3         PC4
+## id                     0.58205514 -0.3385544  0.062469916 -0.10516164
+## prior_prob_count      -0.50027988  0.5120349  0.097545822  0.10595670
+## prior_percent_correct  0.10259004  0.2656193  0.781280744 -0.28670059
+## problems_attempted    -0.24807198 -0.4255122  0.527707102 -0.08843549
+## mean_hint             -0.47081307 -0.3669473 -0.115313460 -0.02244680
+## mean_attempt          -0.34179563 -0.4434016 -0.009533538 -0.27383776
+## mean_confidence       -0.01944837  0.2008274 -0.290378811 -0.90122426
+##                               PC5         PC6          PC7
+## id                    -0.00563084  0.10995181  0.720770551
+## prior_prob_count      -0.02074912 -0.12780116  0.670846181
+## prior_percent_correct  0.29982995  0.34978664 -0.118642884
+## problems_attempted    -0.58767798 -0.35448987 -0.008692926
+## mean_hint             -0.10797157  0.78055600  0.094647492
+## mean_attempt           0.70023474 -0.33740750  0.085558668
+## mean_confidence       -0.24957526 -0.02126521  0.005007913
+
#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
+
+#Now examine your components and try to come up with substantive descriptions of what some might represent?
+
+#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 accounted for. 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)
+

+
pca$rotation
+
##                               PC1        PC2          PC3         PC4
+## id                     0.58205514 -0.3385544  0.062469916 -0.10516164
+## prior_prob_count      -0.50027988  0.5120349  0.097545822  0.10595670
+## prior_percent_correct  0.10259004  0.2656193  0.781280744 -0.28670059
+## problems_attempted    -0.24807198 -0.4255122  0.527707102 -0.08843549
+## mean_hint             -0.47081307 -0.3669473 -0.115313460 -0.02244680
+## mean_attempt          -0.34179563 -0.4434016 -0.009533538 -0.27383776
+## mean_confidence       -0.01944837  0.2008274 -0.290378811 -0.90122426
+##                               PC5         PC6          PC7
+## id                    -0.00563084  0.10995181  0.720770551
+## prior_prob_count      -0.02074912 -0.12780116  0.670846181
+## prior_percent_correct  0.29982995  0.34978664 -0.118642884
+## problems_attempted    -0.58767798 -0.35448987 -0.008692926
+## mean_hint             -0.10797157  0.78055600  0.094647492
+## mean_attempt           0.70023474 -0.33740750  0.085558668
+## mean_confidence       -0.24957526 -0.02126521  0.005007913
+
loadings <- abs(pca$rotation) #abs() will make all eigenvectors positive
+
+
+
+biplot(pca)
+

# Part III
+Also in this repository is a data set collected from TC students (tc-program-combos.csv) that shows how many students thought that a TC program was related to andother TC program. Students were shown three program names at a time and were asked which two of the three were most similar. Use PCA to look for components that represent related programs. Explain why you think there are relationships between these programs.

+
#T<-read.csv("tc-program-combos.csv")
+# library(ggplot2)
+# library(GGally)
+# ggpairs(T, 2:68, progress = FALSE)
+# ggcorr(T[,-1], method = c("everything", "pearson"))
+# pca1 <- prcomp(T[-1], scale. = TRUE)
+# pca1$sdev
+# pca1$sdev^2
+# summary(pca1)
+# plot(pca1, type = "lines")
+
+ + + + +
+ + + + + + + + + + + + + + +