From a54f5a02367ba6cc9aef102aa06102ef9c3ada4b Mon Sep 17 00:00:00 2001 From: He Chen Date: Wed, 25 Nov 2020 16:56:15 -0700 Subject: [PATCH] Finished all problems. Sorry for turning in my homework based on the western time. I forgot the time difference! --- assignment5.Rmd | 31 +++-- assignment5.html | 342 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 361 insertions(+), 12 deletions(-) create mode 100644 assignment5.html diff --git a/assignment5.Rmd b/assignment5.Rmd index 288bcb3..5c14409 100644 --- a/assignment5.Rmd +++ b/assignment5.Rmd @@ -1,5 +1,6 @@ --- title: "Principle Component Aanalysis" +author: 'He Chen' output: html_document --- ## Data @@ -16,7 +17,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') ``` @@ -27,6 +28,7 @@ D1 <- library(ggplot2) library(GGally) +library(tidyverse) 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 @@ -38,7 +40,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 <- select(D1, !c(id,mean_correct)) ``` @@ -67,22 +69,22 @@ plot(pca, type = "lines") ``` ## Decide which components you would drop and remove them from your data set. - +According to the summary, I would like to drop the PC6 becasue it has the smallest relative variance. ## Part II ```{r} #Now, create a data frame of the transformed data from your pca. -D3 <- +D3 <- as.data.frame(pca$x) #Attach the variable "mean_correct" from your original data frame to D3. - +D3 <- D3 %>% mutate(mean_correct = D1$mean_correct) #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? +ggcorr(D3, method = c("everything", "pearson")) - - +# Yes, I would miss the information about the most correlated factor. ``` ## Now print out the loadings for the components you generated: @@ -94,6 +96,9 @@ pca$rotation 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? +loadings + +# Answer: The rotation or the loading scores for each column in different components represents the proportions of each column. For example, if we see 0.633 mean_hint, we would see 0.542 mean hint in PC1. #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. @@ -106,9 +111,11 @@ Also in this repository is a data set collected from TC students (tc-program-com ```{r} -``` - - - - +D4 <- read.csv('tc-program-combos.csv') +D5 <- select(D4, -1) +pca2 <- prcomp(D5, scale. = TRUE) +summary(pca2) +ggcorr(D5, method = c("everything", "pearson")) +# Accoridng to the graph, there are numbers of cells are red, which means they are highly related to each other. +``` diff --git a/assignment5.html b/assignment5.html new file mode 100644 index 0000000..647eb80 --- /dev/null +++ b/assignment5.html @@ -0,0 +1,342 @@ + + + + + + + + + + + + + + +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:

+
    +
  • 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

+
D1 <- read.csv('Assistments-confidence.csv')
+
+
+

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)
+library(tidyverse)
+
## -- Attaching packages ------------------------------------------------------------------------------------ tidyverse 1.3.0 --
+
## v tibble  3.0.3     v dplyr   1.0.2
+## v tidyr   1.1.2     v stringr 1.4.0
+## v readr   1.3.1     v forcats 0.5.0
+## v purrr   0.3.4
+
## -- Conflicts --------------------------------------------------------------------------------------- tidyverse_conflicts() --
+## x dplyr::filter() masks stats::filter()
+## x dplyr::lag()    masks stats::lag()
+
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 <- select(D1, !c(id,mean_correct))
+
+
+

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.2825140 1.0543565 1.0245688 0.9621486 0.8556715 0.7320146
+
#To convert this into variance accounted for we can square it, these numbers are proportional to the eigenvalue
+
+pca$sdev^2
+
## [1] 1.6448423 1.1116675 1.0497412 0.9257299 0.7321737 0.5358454
+
#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
+## Standard deviation     1.2825 1.0544 1.0246 0.9621 0.8557 0.73201
+## Proportion of Variance 0.2741 0.1853 0.1750 0.1543 0.1220 0.08931
+## Cumulative Proportion  0.2741 0.4594 0.6344 0.7887 0.9107 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.

+

According to the summary, I would like to drop the PC6 becasue it has the smallest relative variance. ## Part II

+
#Now, create a data frame of the transformed data from your pca.
+
+D3 <- as.data.frame(pca$x)
+
+#Attach the variable "mean_correct" from your original data frame to D3.
+D3 <- D3 %>% mutate(mean_correct = D1$mean_correct)
+
+
+#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?
+ggcorr(D3, method = c("everything", "pearson"))
+

+
# Yes, I would miss the information about the most correlated factor.
+
+
+

Now print out the loadings for the components you generated:

+
pca$rotation
+
##                               PC1         PC2         PC3        PC4
+## prior_prob_count      -0.26034140  0.45818753 -0.40090679 -0.6897642
+## prior_percent_correct  0.16840319  0.81617867  0.09267306  0.2640040
+## problems_attempted    -0.45568733  0.31685183  0.36387724  0.3168141
+## mean_hint             -0.63337594 -0.12501620 -0.08008842 -0.1122586
+## mean_attempt          -0.54200011 -0.08510858 -0.04585364  0.3108682
+## mean_confidence        0.03581325  0.02547483 -0.83051917  0.4948890
+##                                PC5         PC6
+## prior_prob_count      -0.007142834 -0.29280482
+## prior_percent_correct  0.298843852  0.37134715
+## problems_attempted    -0.592336569 -0.32911025
+## mean_hint             -0.102302115  0.74412634
+## mean_attempt           0.697232132 -0.33781385
+## mean_confidence       -0.251357022 -0.01452143
+
#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?
+loadings
+
##                              PC1        PC2        PC3       PC4
+## prior_prob_count      0.26034140 0.45818753 0.40090679 0.6897642
+## prior_percent_correct 0.16840319 0.81617867 0.09267306 0.2640040
+## problems_attempted    0.45568733 0.31685183 0.36387724 0.3168141
+## mean_hint             0.63337594 0.12501620 0.08008842 0.1122586
+## mean_attempt          0.54200011 0.08510858 0.04585364 0.3108682
+## mean_confidence       0.03581325 0.02547483 0.83051917 0.4948890
+##                               PC5        PC6
+## prior_prob_count      0.007142834 0.29280482
+## prior_percent_correct 0.298843852 0.37134715
+## problems_attempted    0.592336569 0.32911025
+## mean_hint             0.102302115 0.74412634
+## mean_attempt          0.697232132 0.33781385
+## mean_confidence       0.251357022 0.01452143
+
# Answer: The rotation or the loading scores for each column in different components represents the proportions of each column. For example, if we see 0.633 mean_hint, we would see 0.542 mean hint in PC1. 
+
+#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)
+

# 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.

+
D4 <- read.csv('tc-program-combos.csv')
+D5 <- select(D4, -1)
+pca2 <- prcomp(D5, scale. = TRUE)
+summary(pca2)
+
## Importance of components:
+##                           PC1     PC2     PC3     PC4     PC5     PC6
+## Standard deviation     2.6670 2.33303 2.03824 1.80893 1.71451 1.60412
+## Proportion of Variance 0.1062 0.08124 0.06201 0.04884 0.04387 0.03841
+## Cumulative Proportion  0.1062 0.18740 0.24941 0.29825 0.34212 0.38053
+##                            PC7     PC8    PC9    PC10    PC11    PC12
+## Standard deviation     1.58799 1.49222 1.4642 1.39139 1.33521 1.32517
+## Proportion of Variance 0.03764 0.03323 0.0320 0.02889 0.02661 0.02621
+## Cumulative Proportion  0.41816 0.45140 0.4834 0.51229 0.53890 0.56511
+##                          PC13    PC14    PC15    PC16    PC17    PC18
+## Standard deviation     1.3121 1.26312 1.25366 1.22339 1.21896 1.18649
+## Proportion of Variance 0.0257 0.02381 0.02346 0.02234 0.02218 0.02101
+## Cumulative Proportion  0.5908 0.61462 0.63808 0.66042 0.68260 0.70361
+##                          PC19   PC20   PC21    PC22    PC23    PC24
+## Standard deviation     1.1313 1.1281 1.1043 1.06319 1.01168 0.99666
+## Proportion of Variance 0.0191 0.0190 0.0182 0.01687 0.01528 0.01483
+## Cumulative Proportion  0.7227 0.7417 0.7599 0.77678 0.79205 0.80688
+##                           PC25    PC26    PC27    PC28    PC29   PC30
+## Standard deviation     0.96528 0.95049 0.93257 0.90508 0.85161 0.8348
+## Proportion of Variance 0.01391 0.01348 0.01298 0.01223 0.01082 0.0104
+## Cumulative Proportion  0.82079 0.83427 0.84725 0.85948 0.87030 0.8807
+##                           PC31    PC32    PC33    PC34   PC35    PC36
+## Standard deviation     0.81880 0.78539 0.76079 0.73351 0.7228 0.67319
+## Proportion of Variance 0.01001 0.00921 0.00864 0.00803 0.0078 0.00676
+## Cumulative Proportion  0.89071 0.89992 0.90856 0.91659 0.9244 0.93115
+##                           PC37    PC38    PC39    PC40    PC41    PC42
+## Standard deviation     0.66343 0.64839 0.62449 0.60331 0.56847 0.55769
+## Proportion of Variance 0.00657 0.00627 0.00582 0.00543 0.00482 0.00464
+## Cumulative Proportion  0.93772 0.94399 0.94981 0.95524 0.96007 0.96471
+##                           PC43    PC44    PC45    PC46   PC47    PC48
+## Standard deviation     0.51032 0.49443 0.47128 0.44551 0.4329 0.41344
+## Proportion of Variance 0.00389 0.00365 0.00332 0.00296 0.0028 0.00255
+## Cumulative Proportion  0.96860 0.97224 0.97556 0.97852 0.9813 0.98387
+##                           PC49    PC50    PC51    PC52    PC53    PC54
+## Standard deviation     0.37260 0.36654 0.35016 0.33278 0.32800 0.30414
+## Proportion of Variance 0.00207 0.00201 0.00183 0.00165 0.00161 0.00138
+## Cumulative Proportion  0.98594 0.98795 0.98978 0.99143 0.99304 0.99442
+##                           PC55    PC56    PC57    PC58    PC59    PC60
+## Standard deviation     0.28040 0.27067 0.23730 0.21156 0.17617 0.16542
+## Proportion of Variance 0.00117 0.00109 0.00084 0.00067 0.00046 0.00041
+## Cumulative Proportion  0.99559 0.99668 0.99752 0.99819 0.99866 0.99906
+##                           PC61   PC62    PC63    PC64    PC65    PC66
+## Standard deviation     0.14778 0.1420 0.11093 0.07055 0.04430 0.03589
+## Proportion of Variance 0.00033 0.0003 0.00018 0.00007 0.00003 0.00002
+## Cumulative Proportion  0.99939 0.9997 0.99987 0.99995 0.99998 1.00000
+##                           PC67
+## Standard deviation     0.01241
+## Proportion of Variance 0.00000
+## Cumulative Proportion  1.00000
+
ggcorr(D5, method = c("everything", "pearson"))
+

+
# Accoridng to the graph, there are numbers of cells are red, which means they are highly related to each other.
+
+ + + + +
+ + + + + + + +