From ec4e6963ca75316f5ce20b6d8046ea84e55b5a6a Mon Sep 17 00:00:00 2001 From: Sherry Cai <55371672+csherry97@users.noreply.github.com> Date: Thu, 26 Nov 2020 03:16:22 +0800 Subject: [PATCH] Add files via upload --- assignment5.Rmd | 30 ++- assignment5.html | 629 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 653 insertions(+), 6 deletions(-) create mode 100644 assignment5.html diff --git a/assignment5.Rmd b/assignment5.Rmd index 288bcb3..851fd2e 100644 --- a/assignment5.Rmd +++ b/assignment5.Rmd @@ -16,7 +16,10 @@ The data you will be using comes from the Assistments online intelligent tutorin ## Start by uploading the data ```{r} -D1 <- +library(dplyr) +library(tidyr) + +D1 <- read.csv("Assistments-confidence.csv") ``` @@ -38,7 +41,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] ``` @@ -66,22 +69,25 @@ summary(pca) plot(pca, type = "lines") ``` + + ## Decide which components you would drop and remove them from your data set. +## I will remove PCs after PC5 because they have variance less than 1. ## Part II ```{r} #Now, create a data frame of the transformed data from your pca. -D3 <- +D3 <- pca$x[,1:5] #Attach the variable "mean_correct" from your original data frame to D3. - +D3 <- data.frame(D3,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? - - +ggpairs(D3, progress = FALSE) +ggcorr(D3, method = c("everything", "pearson")) ``` ## Now print out the loadings for the components you generated: @@ -105,8 +111,20 @@ 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} +DF1 <- read.csv("tc-program-combos.csv") +ggcorr(DF1[,-1], method = c("everything", "pearson")) +DF2 <- DF1[,-1] +pca2 <- prcomp(DF2, scale. = TRUE) +pca2$sdev +pca2$sdev^2 +summary(pca2) +DF3 <- pca2$x[,1:10] +plot(pca2, type = "lines") +loadings2 <- abs(pca2$rotation) +biplot(pca2) ``` +## We coulad look at loadings2 dataframe and the biplot to see which programs are similar programs. diff --git a/assignment5.html b/assignment5.html new file mode 100644 index 0000000..e53dc8a --- /dev/null +++ b/assignment5.html @@ -0,0 +1,629 @@ + + + + +
+ + + + + + + + +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:
+library(dplyr)
+##
+## Attaching package: 'dplyr'
+## The following objects are masked from 'package:stats':
+##
+## filter, lag
+## The following objects are masked from 'package:base':
+##
+## intersect, setdiff, setequal, union
+library(tidyr)
+
+D1 <- read.csv("Assistments-confidence.csv")
+#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.
+D2 <- D1[,-5]
+pca <- prcomp(D2, scale. = TRUE)
+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")
+#Now, create a data frame of the transformed data from your pca.
+
+D3 <- pca$x[,1:5]
+
+#Attach the variable "mean_correct" from your original data frame to D3.
+
+D3 <- data.frame(D3,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?
+ggpairs(D3, progress = FALSE)
+ggcorr(D3, method = c("everything", "pearson"))
+ ## Now print out the loadings for the components you generated:
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)
+ # 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.
DF1 <- read.csv("tc-program-combos.csv")
+ggcorr(DF1[,-1], method = c("everything", "pearson"))
+DF2 <- DF1[,-1]
+pca2 <- prcomp(DF2, scale. = TRUE)
+pca2$sdev
+## [1] 2.66699514 2.33303087 2.03824332 1.80893489 1.71451092 1.60411744
+## [7] 1.58798960 1.49222150 1.46424566 1.39138869 1.33520786 1.32516917
+## [13] 1.31212863 1.26312253 1.25366254 1.22338737 1.21896028 1.18649390
+## [19] 1.13127469 1.12814038 1.10432926 1.06319093 1.01168384 0.99665812
+## [25] 0.96528383 0.95048841 0.93256896 0.90507518 0.85160824 0.83479067
+## [31] 0.81879538 0.78538963 0.76079365 0.73350908 0.72278124 0.67319166
+## [37] 0.66343310 0.64839067 0.62448974 0.60331242 0.56846989 0.55769066
+## [43] 0.51031628 0.49442626 0.47128286 0.44551299 0.43288829 0.41344476
+## [49] 0.37259965 0.36653720 0.35015529 0.33278376 0.32799766 0.30414406
+## [55] 0.28040415 0.27066834 0.23729873 0.21156010 0.17616906 0.16541514
+## [61] 0.14778289 0.14204907 0.11092615 0.07054620 0.04430024 0.03588606
+## [67] 0.01241193
+pca2$sdev^2
+## [1] 7.1128630864 5.4430330560 4.1544358466 3.2722454371 2.9395476996
+## [6] 2.5731927459 2.5217109546 2.2267250142 2.1440153558 1.9359624893
+## [11] 1.7827800336 1.7560733413 1.7216815395 1.5954785267 1.5716697682
+## [16] 1.4966766496 1.4858641675 1.4077677636 1.2797824290 1.2727007063
+## [21] 1.2195431178 1.1303749458 1.0235041934 0.9933274149 0.9317728773
+## [26] 0.9034282205 0.8696848576 0.8191610815 0.7252366024 0.6968754570
+## [31] 0.6704258671 0.6168368672 0.5788069731 0.5380355701 0.5224127176
+## [36] 0.4531870174 0.4401434846 0.4204104623 0.3899874305 0.3639858772
+## [41] 0.3231580154 0.3110188729 0.2604227077 0.2444573271 0.2221075356
+## [46] 0.1984818264 0.1873922684 0.1709365720 0.1388304984 0.1343495215
+## [51] 0.1226087294 0.1107450296 0.1075824634 0.0925036076 0.0786264855
+## [56] 0.0732613524 0.0563106886 0.0447576745 0.0310355381 0.0273621679
+## [61] 0.0218397813 0.0201779378 0.0123046098 0.0049767662 0.0019625111
+## [66] 0.0012878092 0.0001540561
+summary(pca2)
+## Importance of components:
+## PC1 PC2 PC3 PC4 PC5 PC6 PC7
+## Standard deviation 2.6670 2.33303 2.03824 1.80893 1.71451 1.60412 1.58799
+## Proportion of Variance 0.1062 0.08124 0.06201 0.04884 0.04387 0.03841 0.03764
+## Cumulative Proportion 0.1062 0.18740 0.24941 0.29825 0.34212 0.38053 0.41816
+## PC8 PC9 PC10 PC11 PC12 PC13 PC14
+## Standard deviation 1.49222 1.4642 1.39139 1.33521 1.32517 1.3121 1.26312
+## Proportion of Variance 0.03323 0.0320 0.02889 0.02661 0.02621 0.0257 0.02381
+## Cumulative Proportion 0.45140 0.4834 0.51229 0.53890 0.56511 0.5908 0.61462
+## PC15 PC16 PC17 PC18 PC19 PC20 PC21
+## Standard deviation 1.25366 1.22339 1.21896 1.18649 1.1313 1.1281 1.1043
+## Proportion of Variance 0.02346 0.02234 0.02218 0.02101 0.0191 0.0190 0.0182
+## Cumulative Proportion 0.63808 0.66042 0.68260 0.70361 0.7227 0.7417 0.7599
+## PC22 PC23 PC24 PC25 PC26 PC27 PC28
+## Standard deviation 1.06319 1.01168 0.99666 0.96528 0.95049 0.93257 0.90508
+## Proportion of Variance 0.01687 0.01528 0.01483 0.01391 0.01348 0.01298 0.01223
+## Cumulative Proportion 0.77678 0.79205 0.80688 0.82079 0.83427 0.84725 0.85948
+## PC29 PC30 PC31 PC32 PC33 PC34 PC35
+## Standard deviation 0.85161 0.8348 0.81880 0.78539 0.76079 0.73351 0.7228
+## Proportion of Variance 0.01082 0.0104 0.01001 0.00921 0.00864 0.00803 0.0078
+## Cumulative Proportion 0.87030 0.8807 0.89071 0.89992 0.90856 0.91659 0.9244
+## PC36 PC37 PC38 PC39 PC40 PC41 PC42
+## Standard deviation 0.67319 0.66343 0.64839 0.62449 0.60331 0.56847 0.55769
+## Proportion of Variance 0.00676 0.00657 0.00627 0.00582 0.00543 0.00482 0.00464
+## Cumulative Proportion 0.93115 0.93772 0.94399 0.94981 0.95524 0.96007 0.96471
+## PC43 PC44 PC45 PC46 PC47 PC48 PC49
+## Standard deviation 0.51032 0.49443 0.47128 0.44551 0.4329 0.41344 0.37260
+## Proportion of Variance 0.00389 0.00365 0.00332 0.00296 0.0028 0.00255 0.00207
+## Cumulative Proportion 0.96860 0.97224 0.97556 0.97852 0.9813 0.98387 0.98594
+## PC50 PC51 PC52 PC53 PC54 PC55 PC56
+## Standard deviation 0.36654 0.35016 0.33278 0.32800 0.30414 0.28040 0.27067
+## Proportion of Variance 0.00201 0.00183 0.00165 0.00161 0.00138 0.00117 0.00109
+## Cumulative Proportion 0.98795 0.98978 0.99143 0.99304 0.99442 0.99559 0.99668
+## PC57 PC58 PC59 PC60 PC61 PC62 PC63
+## Standard deviation 0.23730 0.21156 0.17617 0.16542 0.14778 0.1420 0.11093
+## Proportion of Variance 0.00084 0.00067 0.00046 0.00041 0.00033 0.0003 0.00018
+## Cumulative Proportion 0.99752 0.99819 0.99866 0.99906 0.99939 0.9997 0.99987
+## PC64 PC65 PC66 PC67
+## Standard deviation 0.07055 0.04430 0.03589 0.01241
+## Proportion of Variance 0.00007 0.00003 0.00002 0.00000
+## Cumulative Proportion 0.99995 0.99998 1.00000 1.00000
+DF3 <- pca2$x[,1:10]
+plot(pca2, type = "lines")
+loadings2 <- abs(pca2$rotation)
+biplot(pca2)
+ ## We coulad look at loadings2 dataframe and the biplot to see which programs are similar programs.