From 4a7c40dce1f6ccd556d3ce52d771b434e9b79a9c Mon Sep 17 00:00:00 2001 From: Yen-Ling Date: Wed, 25 Nov 2020 14:09:33 -0800 Subject: [PATCH] Yen-Ling Cheng Assignment. --- assignment5-yenling.Rmd | 159 ++++++++ assignment5-yenling.html | 850 +++++++++++++++++++++++++++++++++++++++ assignment5.Rmd | 61 ++- assignment5.html | 696 ++++++++++++++++++++++++++++++++ 4 files changed, 1758 insertions(+), 8 deletions(-) create mode 100644 assignment5-yenling.Rmd create mode 100644 assignment5-yenling.html create mode 100644 assignment5.html diff --git a/assignment5-yenling.Rmd b/assignment5-yenling.Rmd new file mode 100644 index 0000000..e0dbced --- /dev/null +++ b/assignment5-yenling.Rmd @@ -0,0 +1,159 @@ +--- +title: "Principle Component Aanalysis" +output: html_document +--- +## 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 +```{r} +D1 <- read.csv("Assistments-confidence.csv",header = TRUE) + +``` + +## 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(ggplot2) +library(GGally) + +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. + +#I found that mean_correct is highly related to prior_percent_correct and mean_hint. +``` + +## 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 <- subset(D1, select = -(mean_correct)) +``` + +## Now run the PCA on the new data frame + +```{r} +pca <- prcomp(D2[ , 2:7], 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. + +```{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 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. + +I would like to drop PC4, PC5, PC6, PC7 because the explained variation is less than 1 . From the plot, we can infer that the slope begins decreasing from PC4. +```{r} + +``` + + +## Part II + +```{r} +#Now, create a data frame of the transformed data from your pca. + +D3 <- data.frame(pca$x) + +#drop pc4, pc5 and pc6 +D3_2<-D3[, 1:3] +D3_2<-data.frame(D3_2, D1$mean_correct) +names(D3_2)[4]<-"mean_correct" + +#Attach the variable "mean_correct" from your original data frame to D3. + +D3 <- data.frame(D3, D1$mean_correct) +names(D3)[7] <-"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) +ggpairs(D3_2,progress = FALSE) + +#mean_correct is significantly correlated with PC1(r=.28, p<.001), PC2(r=.37, p<.001), PC4(r=.13, p<.05), and PC6(r=-.39, p<.001). If we drop PC4, PC5, and PC6, we would not be able to spot the associations bwtween mean_correct and PC4 & PC6 + +``` +## 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 + +#abs() will make all eigenvectors positive +loadings <- abs(pca$rotation) + +#Now examine your components and try to come up with substantive descriptions of what some might represent? + +#PC 1 has a strong negative loading for mean_hint and mean attempt, and positive loadings for prior_percent_correct. These variables stand for the performance in current session. PC 2 is dominated by large positive loadings for prior_percent_correct and positive loadings for prior_prob_count. As these are analyses of students' working through online math problems which may reflect students’ prior experiences to the current session. PC 3 is dominated by large negative loadings for mean_confidence, so this might reflect the confidence of students in solving math problems. As for PC4 to PC 6, the factor loadings of these components don't show distinct patterns so it is challenging to tell what these components might represent. In addition, the contributed variance is not high. As a result, it is difficult to provide theoretical intepretations for these components. + + +#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. + +```{r} +TC <- read.csv("tc-program-combos.csv", header = TRUE) +TC <- TC[-c(69),] + +#pca +pca_TC <- prcomp(TC[, 2:68], scale. = TRUE) +plot(pca_TC, type = "line") + +#Choose PC1 to PC4 as major components. Although we can see that the eigenvalues from PC1 to PC24 are larger than 1, we can still see from the plot where the slope starts to decrease from PC4. So I decide to choose PC1 to PC4. + +pca_TC$rotation[ ,1:4] + +#create a data frame for analysis +PCAIII <- data.frame(abs(pca_TC$rotation[ ,1:4])) + +#From the results, we can see that PC1 has larger loadings for change_leadership, Economics.and.Education, Education.Policy, Arts.Administration, School.Principals, Social.Organizational.Psychology. So we can know that PC1 might stand for programs related to administration, school policy and leadership. PC2 has stronger loadings for Clinical.Psychology, Neuroscience, Kinesiology, Physiology, Psychology, Health.Education, Nursing and Behavior.Analysis. So it might represent the health-related programs. PC3 has stronger loadings for Design.and.Development.of.Digital.Games,Cognitive.Science, Learning.Analytics, Mathematics, Education.Technology, Creative.Technologies, Instructional.Technology.and.Media, Measurement.Evaluation.and.Statistics and Communication.Media.and.Learning.Technologies. PC3 is more related to math and technology or STEM fields. PC4 has higher loadings for Linguistics, English.Education, Teaching.English, Literacy, Deaf.and.Hard.of.Hearing. We can infer from the results that PC4 might be more related to language learning. + + +``` + + + + + diff --git a/assignment5-yenling.html b/assignment5-yenling.html new file mode 100644 index 0000000..343e26f --- /dev/null +++ b/assignment5-yenling.html @@ -0,0 +1,850 @@ + + + + + + + + + + + + + +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",header = TRUE)
+
+
+

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. 
+
+#I found that mean_correct is highly related to prior_percent_correct and mean_hint.
+
+
+

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 <- subset(D1, select = -(mean_correct))
+
+
+

Now run the PCA on the new data frame

+
pca <- prcomp(D2[ , 2:7], 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.

+

I would like to drop PC4, PC5, PC6, PC7 because the explained variation is less than 1 . From the plot, we can infer that the slope begins decreasing from PC4.

+
+
+

Part II

+
#Now, create a data frame of the transformed data from your pca.
+
+D3 <- data.frame(pca$x)
+
+#drop pc4, pc5 and pc6
+D3_2<-D3[, 1:3]
+D3_2<-data.frame(D3_2, D1$mean_correct)
+names(D3_2)[4]<-"mean_correct"
+
+#Attach the variable "mean_correct" from your original data frame to D3.
+
+D3 <- data.frame(D3, D1$mean_correct)
+names(D3)[7] <-"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)
+

+
ggpairs(D3_2,progress = FALSE)
+

+
#mean_correct is significantly correlated with PC1(r=.28, p<.001), PC2(r=.37, p<.001), PC4(r=.13, p<.05), and PC6(r=-.39, p<.001). If we drop PC4, PC5, and PC6, we would not be able to spot the associations bwtween mean_correct and PC4 & PC6
+
+
+

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
+
+#abs() will make all eigenvectors positive
+loadings <- abs(pca$rotation)
+
+#Now examine your components and try to come up with substantive descriptions of what some might represent?
+
+#PC 1 has a strong negative loading for mean_hint and mean attempt, and positive loadings for prior_percent_correct. These variables stand for the performance in current session. PC 2 is dominated by large positive loadings for prior_percent_correct and positive loadings for prior_prob_count. As these are analyses of students' working through online math problems which may reflect students’ prior experiences to the current session. PC 3 is dominated by large negative loadings for mean_confidence, so this might reflect the confidence of students in solving math problems. As for PC4 to PC 6, the factor loadings of these components don't show distinct patterns so it is challenging to tell what these components might represent. In addition, the contributed variance is not high. As a result, it is difficult to provide theoretical intepretations for these components.
+
+
+#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.

+
TC <- read.csv("tc-program-combos.csv", header = TRUE)
+TC <- TC[-c(69),]
+summary(TC)
+
##    program          Adult.Education   Anthropology    Social.Studies  
+##  Length:68          Min.   :0.0000   Min.   :0.0000   Min.   :0.0000  
+##  Class :character   1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.0000  
+##  Mode  :character   Median :1.0000   Median :0.0000   Median :0.0000  
+##                     Mean   :0.8235   Mean   :0.8235   Mean   :0.8235  
+##                     3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000  
+##                     Max.   :5.0000   Max.   :5.0000   Max.   :6.0000  
+##    Physiology     Behavior.Analysis  Linguistics     Art.Education   
+##  Min.   :0.0000   Min.   :0.0000    Min.   :0.0000   Min.   :0.0000  
+##  1st Qu.:0.0000   1st Qu.:0.0000    1st Qu.:0.0000   1st Qu.:0.0000  
+##  Median :0.0000   Median :0.0000    Median :0.0000   Median :0.0000  
+##  Mean   :0.8235   Mean   :0.8235    Mean   :0.8235   Mean   :0.8235  
+##  3rd Qu.:1.0000   3rd Qu.:1.0000    3rd Qu.:1.0000   3rd Qu.:1.0000  
+##  Max.   :5.0000   Max.   :5.0000    Max.   :9.0000   Max.   :6.0000  
+##  Teaching.English Arts.Administration Bilingual.Bicultural.Education
+##  Min.   :0.0000   Min.   :0.0000      Min.   :0.0000                
+##  1st Qu.:0.0000   1st Qu.:0.0000      1st Qu.:0.0000                
+##  Median :0.0000   Median :0.0000      Median :0.0000                
+##  Mean   :0.8235   Mean   :0.8235      Mean   :0.8088                
+##  3rd Qu.:1.0000   3rd Qu.:1.0000      3rd Qu.:1.0000                
+##  Max.   :6.0000   Max.   :7.0000      Max.   :5.0000                
+##  Clinical.Psychology Cognitive.Science College.Advising
+##  Min.   :0.0000      Min.   :0.0000    Min.   :0.0000  
+##  1st Qu.:0.0000      1st Qu.:0.0000    1st Qu.:0.0000  
+##  Median :0.0000      Median :0.0000    Median :0.0000  
+##  Mean   :0.8235      Mean   :0.8235    Mean   :0.8088  
+##  3rd Qu.:1.0000      3rd Qu.:1.0000    3rd Qu.:1.0000  
+##  Max.   :6.0000      Max.   :4.0000    Max.   :4.0000  
+##  Communication.Sciences.and.Disorders Cooperation.and.Conflict.Resolution
+##  Min.   :0.0000                       Min.   :0.0000                     
+##  1st Qu.:0.0000                       1st Qu.:0.0000                     
+##  Median :0.0000                       Median :0.0000                     
+##  Mean   :0.8235                       Mean   :0.8235                     
+##  3rd Qu.:1.0000                       3rd Qu.:2.0000                     
+##  Max.   :4.0000                       Max.   :4.0000                     
+##  Creative.Technologies Curriculum.and.Teaching Dance.Education 
+##  Min.   : 0.0000       Min.   :0.0000          Min.   :0.0000  
+##  1st Qu.: 0.0000       1st Qu.:0.0000          1st Qu.:0.0000  
+##  Median : 0.0000       Median :0.0000          Median :0.0000  
+##  Mean   : 0.8235       Mean   :0.7941          Mean   :0.8235  
+##  3rd Qu.: 1.0000       3rd Qu.:1.0000          3rd Qu.:1.0000  
+##  Max.   :14.0000       Max.   :6.0000          Max.   :4.0000  
+##  Deaf.and.Hard.of.Hearing Design.and.Development.of.Digital.Games
+##  Min.   :0.0000           Min.   :0.0000                         
+##  1st Qu.:0.0000           1st Qu.:0.0000                         
+##  Median :0.0000           Median :0.0000                         
+##  Mean   :0.8235           Mean   :0.8235                         
+##  3rd Qu.:1.0000           3rd Qu.:1.0000                         
+##  Max.   :4.0000           Max.   :7.0000                         
+##  Developmental.Psychology Diabetes.Education Early.Childhood.Education
+##  Min.   :0.0000           Min.   :0.0000     Min.   :0.0000           
+##  1st Qu.:0.0000           1st Qu.:0.0000     1st Qu.:0.0000           
+##  Median :0.5000           Median :0.0000     Median :0.0000           
+##  Mean   :0.8235           Mean   :0.8235     Mean   :0.8235           
+##  3rd Qu.:1.0000           3rd Qu.:1.0000     3rd Qu.:1.0000           
+##  Max.   :5.0000           Max.   :6.0000     Max.   :5.0000           
+##  Early.Childhood.Special.Education Economics.and.Education Education.Technology
+##  Min.   :0.0000                    Min.   :0.0000          Min.   :0.0000      
+##  1st Qu.:0.0000                    1st Qu.:0.0000          1st Qu.:0.0000      
+##  Median :1.0000                    Median :0.0000          Median :1.0000      
+##  Mean   :0.8235                    Mean   :0.8235          Mean   :0.8235      
+##  3rd Qu.:1.0000                    3rd Qu.:1.0000          3rd Qu.:1.0000      
+##  Max.   :3.0000                    Max.   :6.0000          Max.   :4.0000      
+##  Education.Leadership Education.Policy Inclusive.Education English.Education
+##  Min.   :0.0000       Min.   :0.0000   Min.   :0.0000      Min.   :0.0000   
+##  1st Qu.:0.0000       1st Qu.:0.0000   1st Qu.:0.0000      1st Qu.:0.0000   
+##  Median :0.0000       Median :1.0000   Median :1.0000      Median :0.0000   
+##  Mean   :0.8235       Mean   :0.8235   Mean   :0.8235      Mean   :0.8235   
+##  3rd Qu.:1.0000       3rd Qu.:1.0000   3rd Qu.:1.0000      3rd Qu.:1.0000   
+##  Max.   :5.0000       Max.   :4.0000   Max.   :5.0000      Max.   :6.0000   
+##  Change.Leadership    Nursing       Gifted.Education Health.Education
+##  Min.   :0.0000    Min.   :0.0000   Min.   :0.0000   Min.   :0.0000  
+##  1st Qu.:0.0000    1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.0000  
+##  Median :0.0000    Median :0.0000   Median :0.0000   Median :0.5000  
+##  Mean   :0.7941    Mean   :0.8088   Mean   :0.8088   Mean   :0.8088  
+##  3rd Qu.:1.0000    3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000  
+##  Max.   :6.0000    Max.   :7.0000   Max.   :7.0000   Max.   :4.0000  
+##  Higher.and.Postsecondary.Education    History      
+##  Min.   :0.0000                     Min.   :0.0000  
+##  1st Qu.:0.0000                     1st Qu.:0.0000  
+##  Median :1.0000                     Median :0.0000  
+##  Mean   :0.8088                     Mean   :0.8088  
+##  3rd Qu.:1.0000                     3rd Qu.:1.0000  
+##  Max.   :3.0000                     Max.   :7.0000  
+##  Instructional.Technology.and.Media Intellectual.Disability.Autism
+##  Min.   :0.0000                     Min.   :0.0000                
+##  1st Qu.:0.0000                     1st Qu.:0.0000                
+##  Median :1.0000                     Median :1.0000                
+##  Mean   :0.8088                     Mean   :0.8088                
+##  3rd Qu.:1.0000                     3rd Qu.:1.0000                
+##  Max.   :3.0000                     Max.   :4.0000                
+##  International.and.Comparative.Education  Kinesiology     Learning.Analytics
+##  Min.   :0.0000                          Min.   :0.0000   Min.   :0.0000    
+##  1st Qu.:0.0000                          1st Qu.:0.0000   1st Qu.:0.0000    
+##  Median :0.0000                          Median :1.0000   Median :0.0000    
+##  Mean   :0.8088                          Mean   :0.8088   Mean   :0.7941    
+##  3rd Qu.:1.2500                          3rd Qu.:1.0000   3rd Qu.:1.0000    
+##  Max.   :4.0000                          Max.   :5.0000   Max.   :6.0000    
+##     Literacy       Mathematics     Measurement..Evaluation.and.Statistics
+##  Min.   :0.0000   Min.   :0.0000   Min.   :0.0000                        
+##  1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.0000                        
+##  Median :0.0000   Median :0.0000   Median :0.0000                        
+##  Mean   :0.8088   Mean   :0.8088   Mean   :0.8088                        
+##  3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000                        
+##  Max.   :8.0000   Max.   :5.0000   Max.   :8.0000                        
+##  Motor.Learning.and.Control Movement.Science     Music         Neuroscience   
+##  Min.   :0.0000             Min.   :0.0000   Min.   :0.0000   Min.   :0.0000  
+##  1st Qu.:0.0000             1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.0000  
+##  Median :1.0000             Median :0.0000   Median :0.0000   Median :0.0000  
+##  Mean   :0.8088             Mean   :0.8088   Mean   :0.7941   Mean   :0.8088  
+##  3rd Qu.:1.0000             3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000  
+##  Max.   :4.0000             Max.   :4.0000   Max.   :3.0000   Max.   :5.0000  
+##    Nutrition        Leadership       Philosophy     Physical.Education
+##  Min.   :0.0000   Min.   :0.0000   Min.   :0.0000   Min.   :0.0000    
+##  1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.0000    
+##  Median :0.0000   Median :0.0000   Median :1.0000   Median :0.5000    
+##  Mean   :0.7941   Mean   :0.8088   Mean   :0.8088   Mean   :0.8088    
+##  3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000    
+##  Max.   :9.0000   Max.   :4.0000   Max.   :4.0000   Max.   :4.0000    
+##     Politics      Private.School.Leadership Psychological.Counseling
+##  Min.   :0.0000   Min.   :0.0000            Min.   :0.0000          
+##  1st Qu.:0.0000   1st Qu.:0.0000            1st Qu.:0.0000          
+##  Median :0.0000   Median :0.5000            Median :0.0000          
+##  Mean   :0.8088   Mean   :0.8088            Mean   :0.8088          
+##  3rd Qu.:1.0000   3rd Qu.:1.0000            3rd Qu.:1.0000          
+##  Max.   :5.0000   Max.   :4.0000            Max.   :4.0000          
+##    Psychology        Reading       School.Psychology Science.Education
+##  Min.   :0.0000   Min.   :0.0000   Min.   :0.0000    Min.   :0.0000   
+##  1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.0000    1st Qu.:0.0000   
+##  Median :0.0000   Median :1.0000   Median :1.0000    Median :0.0000   
+##  Mean   :0.8088   Mean   :0.8088   Mean   :0.7941    Mean   :0.8088   
+##  3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000    3rd Qu.:1.0000   
+##  Max.   :5.0000   Max.   :4.0000   Max.   :4.0000    Max.   :5.0000   
+##  Sexuality..Women.and.Gender.in.Psychology Social.Organizational.Psychology
+##  Min.   :0.0000                            Min.   :0.0000                  
+##  1st Qu.:0.0000                            1st Qu.:0.0000                  
+##  Median :0.0000                            Median :1.0000                  
+##  Mean   :0.8088                            Mean   :0.7941                  
+##  3rd Qu.:1.2500                            3rd Qu.:1.0000                  
+##  Max.   :3.0000                            Max.   :6.0000                  
+##    Sociology      Spirituality.Mind.Body School.Principals Urban.Education 
+##  Min.   :0.0000   Min.   :0.0000         Min.   :0.0000    Min.   :0.0000  
+##  1st Qu.:0.0000   1st Qu.:0.0000         1st Qu.:0.0000    1st Qu.:0.0000  
+##  Median :0.5000   Median :1.0000         Median :0.0000    Median :1.0000  
+##  Mean   :0.8088   Mean   :0.7941         Mean   :0.8088    Mean   :0.8088  
+##  3rd Qu.:1.0000   3rd Qu.:1.0000         3rd Qu.:1.0000    3rd Qu.:1.0000  
+##  Max.   :4.0000   Max.   :3.0000         Max.   :6.0000    Max.   :6.0000  
+##  Counseling.Psychology Communication.Media.and.Learning.Technologies
+##  Min.   :0.0000        Min.   :0.0000                               
+##  1st Qu.:0.0000        1st Qu.:0.0000                               
+##  Median :0.0000        Median :0.0000                               
+##  Mean   :0.8235        Mean   :0.8235                               
+##  3rd Qu.:1.0000        3rd Qu.:1.0000                               
+##  Max.   :5.0000        Max.   :6.0000
+
#pca
+pca_TC <- prcomp(TC[, 2:68], scale. = TRUE)
+plot(pca_TC, type = "line")
+

+
#Choose PC1 to PC4 as major components. Although we can see that the eigenvalues from PC1 to PC24 are larger than 1, we can still see from the plot where the slope starts to decrease from PC4. So I decide to choose PC1 to PC4.
+
+pca_TC$rotation[ ,1:4]
+
##                                                        PC1          PC2
+## Adult.Education                                0.194907620 -0.083660702
+## Anthropology                                   0.041360196  0.066809252
+## Social.Studies                                 0.117837722 -0.010380697
+## Physiology                                    -0.108687405  0.244542326
+## Behavior.Analysis                              0.073661207  0.211440119
+## Linguistics                                    0.030936146 -0.058129368
+## Art.Education                                 -0.101375553 -0.009569729
+## Teaching.English                               0.095633847 -0.080037587
+## Arts.Administration                            0.219060558 -0.009467423
+## Bilingual.Bicultural.Education                 0.186769330 -0.090013968
+## Clinical.Psychology                            0.137768455  0.252356427
+## Cognitive.Science                             -0.003918107  0.137624238
+## College.Advising                               0.136324871 -0.070307624
+## Communication.Sciences.and.Disorders           0.076814691  0.151509514
+## Cooperation.and.Conflict.Resolution            0.204359318  0.072236705
+## Creative.Technologies                         -0.033911621 -0.040467901
+## Curriculum.and.Teaching                        0.046003116 -0.145594420
+## Dance.Education                               -0.018044738  0.031049590
+## Deaf.and.Hard.of.Hearing                      -0.010396059  0.190060635
+## Design.and.Development.of.Digital.Games       -0.032589151 -0.074604779
+## Developmental.Psychology                       0.081965256  0.151561100
+## Diabetes.Education                            -0.018253473  0.190171034
+## Early.Childhood.Education                      0.108108109 -0.022038878
+## Early.Childhood.Special.Education              0.011567112  0.140539711
+## Economics.and.Education                        0.230528054 -0.046015056
+## Education.Technology                           0.079829536 -0.072886402
+## Education.Leadership                           0.164432407 -0.021728410
+## Education.Policy                               0.220770376 -0.051250478
+## Inclusive.Education                            0.112380969  0.077137421
+## English.Education                              0.057473723 -0.041394592
+## Change.Leadership                              0.278930565  0.026329974
+## Nursing                                       -0.003854528  0.212602854
+## Gifted.Education                               0.060635617  0.034850339
+## Health.Education                              -0.069862687  0.222003175
+## Higher.and.Postsecondary.Education             0.049524691 -0.087291131
+## History                                        0.185629885  0.006006837
+## Instructional.Technology.and.Media             0.015320724  0.036757032
+## Intellectual.Disability.Autism                -0.037790353  0.056188222
+## International.and.Comparative.Education        0.147908115 -0.136693075
+## Kinesiology                                   -0.073255268  0.251470260
+## Learning.Analytics                             0.051252718  0.024021348
+## Literacy                                       0.101560946 -0.055550280
+## Mathematics                                   -0.022974286  0.017848432
+## Measurement..Evaluation.and.Statistics         0.053783158  0.025116436
+## Motor.Learning.and.Control                    -0.067528081  0.065840399
+## Movement.Science                              -0.093522663  0.154922264
+## Music                                          0.020618416 -0.037701230
+## Neuroscience                                   0.050526637  0.252119919
+## Nutrition                                     -0.096385849  0.100764683
+## Leadership                                     0.200003803 -0.043532302
+## Philosophy                                     0.049996731 -0.034949302
+## Physical.Education                            -0.005433719  0.210486469
+## Politics                                       0.218057264 -0.092737673
+## Private.School.Leadership                      0.204088360 -0.045205016
+## Psychological.Counseling                       0.022067733  0.180650487
+## Psychology                                     0.132906972  0.222443155
+## Reading                                        0.141097198  0.007766273
+## School.Psychology                              0.090602812  0.072743648
+## Science.Education                              0.002684045 -0.040137732
+## Sexuality..Women.and.Gender.in.Psychology      0.138506287  0.190610919
+## Social.Organizational.Psychology               0.214808381  0.065416987
+## Sociology                                      0.139463117  0.172151333
+## Spirituality.Mind.Body                         0.121844511  0.178198807
+## School.Principals                              0.217035061 -0.025926133
+## Urban.Education                                0.197396920 -0.005648150
+## Counseling.Psychology                          0.070050239  0.194279060
+## Communication.Media.and.Learning.Technologies  0.055551641  0.029954147
+##                                                        PC3          PC4
+## Adult.Education                                0.023445347 -0.001779881
+## Anthropology                                  -0.016237112 -0.187213616
+## Social.Studies                                -0.059407968 -0.065470345
+## Physiology                                    -0.028047691  0.084633393
+## Behavior.Analysis                              0.064098874  0.066371199
+## Linguistics                                    0.103870137 -0.349415088
+## Art.Education                                  0.042578385 -0.033737983
+## Teaching.English                               0.051025590 -0.269483354
+## Arts.Administration                            0.114963460 -0.028871393
+## Bilingual.Bicultural.Education                -0.008177843 -0.184820488
+## Clinical.Psychology                           -0.065226181 -0.040323389
+## Cognitive.Science                              0.310049646 -0.049256125
+## College.Advising                              -0.092884496  0.102227070
+## Communication.Sciences.and.Disorders           0.057656692 -0.171412811
+## Cooperation.and.Conflict.Resolution            0.011824946  0.044503086
+## Creative.Technologies                          0.257902454 -0.047398824
+## Curriculum.and.Teaching                       -0.031482057 -0.141613230
+## Dance.Education                                0.142857775 -0.129675555
+## Deaf.and.Hard.of.Hearing                       0.010521014 -0.226471838
+## Design.and.Development.of.Digital.Games        0.311811509 -0.157136368
+## Developmental.Psychology                      -0.021652316  0.021750739
+## Diabetes.Education                            -0.065866033 -0.056306543
+## Early.Childhood.Education                      0.011078506 -0.127877610
+## Early.Childhood.Special.Education             -0.113006561 -0.165963173
+## Economics.and.Education                       -0.038509679  0.153925687
+## Education.Technology                           0.267265344  0.122994522
+## Education.Leadership                          -0.020704352  0.142784862
+## Education.Policy                              -0.065158499  0.071827191
+## Inclusive.Education                           -0.042312265  0.029020546
+## English.Education                              0.024145476 -0.337399324
+## Change.Leadership                              0.033050552  0.083651439
+## Nursing                                       -0.121033891 -0.008624699
+## Gifted.Education                               0.140880110 -0.009719421
+## Health.Education                              -0.146182194  0.040814576
+## Higher.and.Postsecondary.Education            -0.046200849  0.053065587
+## History                                       -0.027254571 -0.143212616
+## Instructional.Technology.and.Media             0.257463729 -0.039080666
+## Intellectual.Disability.Autism                -0.061585850 -0.104119711
+## International.and.Comparative.Education        0.009943997  0.068314251
+## Kinesiology                                    0.034908809  0.027182635
+## Learning.Analytics                             0.287201552  0.082142410
+## Literacy                                       0.017567595 -0.252038629
+## Mathematics                                    0.280551662  0.157390715
+## Measurement..Evaluation.and.Statistics         0.250418937  0.107224362
+## Motor.Learning.and.Control                     0.159382979  0.153013712
+## Movement.Science                               0.055769506  0.051741949
+## Music                                          0.122488030 -0.048894835
+## Neuroscience                                   0.127890318  0.017913724
+## Nutrition                                     -0.113547665 -0.066261396
+## Leadership                                    -0.087293702  0.178734295
+## Philosophy                                    -0.013583324  0.016072657
+## Physical.Education                             0.071997259  0.044136909
+## Politics                                      -0.046830268  0.113182579
+## Private.School.Leadership                      0.021496564  0.087300718
+## Psychological.Counseling                      -0.061950759 -0.125561810
+## Psychology                                     0.140185172  0.053162628
+## Reading                                        0.043409848 -0.167814085
+## School.Psychology                             -0.047740125 -0.028256768
+## Science.Education                              0.140867467 -0.011956768
+## Sexuality..Women.and.Gender.in.Psychology     -0.015739877  0.016023814
+## Social.Organizational.Psychology              -0.091221647 -0.075235719
+## Sociology                                     -0.084962825  0.132987962
+## Spirituality.Mind.Body                        -0.059166422 -0.106321742
+## School.Principals                             -0.044666191  0.007921960
+## Urban.Education                                0.006281815 -0.022259541
+## Counseling.Psychology                          0.061477795  0.013321688
+## Communication.Media.and.Learning.Technologies  0.224566731  0.055796280
+
#create a data frame for analysis
+PCAIII <- data.frame(abs(pca_TC$rotation[ ,1:4]))
+
+#From the results, we can see that PC1 has larger loadings for change_leadership, Economics.and.Education, Education.Policy, Arts.Administration, School.Principals, Social.Organizational.Psychology. So we can know that PC1 might stand for programs related to administration, school policy and leadership. PC2 has stronger loadings for Clinical.Psychology, Neuroscience, Kinesiology, Physiology, Psychology, Health.Education, Nursing and Behavior.Analysis. So it might represent the health-related programs. PC3 has stronger loadings for Design.and.Development.of.Digital.Games,Cognitive.Science, Learning.Analytics, Mathematics, Education.Technology, Creative.Technologies, Instructional.Technology.and.Media, Measurement.Evaluation.and.Statistics and Communication.Media.and.Learning.Technologies. PC3 is more related to math and technology or STEM fields. PC4 has higher loadings for Linguistics, English.Education, Teaching.English, Literacy, Deaf.and.Hard.of.Hearing. We can infer from the results that PC4 might be more related to language learning.
+
+ + + + +
+ + + + + + + + + + + + + + + diff --git a/assignment5.Rmd b/assignment5.Rmd index 288bcb3..e0dbced 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 = TRUE) ``` @@ -28,29 +28,36 @@ D1 <- library(ggplot2) library(GGally) -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 +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") + +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. + +#I found that mean_correct is highly related to prior_percent_correct and mean_hint. ``` ## 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 <- subset(D1, select = -(mean_correct)) ``` ## Now run the PCA on the new data frame ```{r} -pca <- prcomp(D2, scale. = TRUE) +pca <- prcomp(D2[ , 2:7], 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. ```{r} + pca$sdev #To convert this into variance accounted for we can square it, these numbers are proportional to the eigenvalue @@ -64,37 +71,59 @@ summary(pca) #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. +I would like to drop PC4, PC5, PC6, PC7 because the explained variation is less than 1 . From the plot, we can infer that the slope begins decreasing from PC4. +```{r} + +``` + + ## Part II ```{r} #Now, create a data frame of the transformed data from your pca. -D3 <- +D3 <- data.frame(pca$x) -#Attach the variable "mean_correct" from your original data frame to D3. +#drop pc4, pc5 and pc6 +D3_2<-D3[, 1:3] +D3_2<-data.frame(D3_2, D1$mean_correct) +names(D3_2)[4]<-"mean_correct" +#Attach the variable "mean_correct" from your original data frame to D3. +D3 <- data.frame(D3, D1$mean_correct) +names(D3)[7] <-"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) +ggpairs(D3_2,progress = FALSE) +#mean_correct is significantly correlated with PC1(r=.28, p<.001), PC2(r=.37, p<.001), PC4(r=.13, p<.05), and PC6(r=-.39, p<.001). If we drop PC4, PC5, and PC6, we would not be able to spot the associations bwtween mean_correct and PC4 & PC6 ``` ## 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 +#abs() will make all eigenvectors positive +loadings <- abs(pca$rotation) #Now examine your components and try to come up with substantive descriptions of what some might represent? +#PC 1 has a strong negative loading for mean_hint and mean attempt, and positive loadings for prior_percent_correct. These variables stand for the performance in current session. PC 2 is dominated by large positive loadings for prior_percent_correct and positive loadings for prior_prob_count. As these are analyses of students' working through online math problems which may reflect students’ prior experiences to the current session. PC 3 is dominated by large negative loadings for mean_confidence, so this might reflect the confidence of students in solving math problems. As for PC4 to PC 6, the factor loadings of these components don't show distinct patterns so it is challenging to tell what these components might represent. In addition, the contributed variance is not high. As a result, it is difficult to provide theoretical intepretations for these components. + + #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 +134,22 @@ 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} +TC <- read.csv("tc-program-combos.csv", header = TRUE) +TC <- TC[-c(69),] + +#pca +pca_TC <- prcomp(TC[, 2:68], scale. = TRUE) +plot(pca_TC, type = "line") + +#Choose PC1 to PC4 as major components. Although we can see that the eigenvalues from PC1 to PC24 are larger than 1, we can still see from the plot where the slope starts to decrease from PC4. So I decide to choose PC1 to PC4. + +pca_TC$rotation[ ,1:4] + +#create a data frame for analysis +PCAIII <- data.frame(abs(pca_TC$rotation[ ,1:4])) + +#From the results, we can see that PC1 has larger loadings for change_leadership, Economics.and.Education, Education.Policy, Arts.Administration, School.Principals, Social.Organizational.Psychology. So we can know that PC1 might stand for programs related to administration, school policy and leadership. PC2 has stronger loadings for Clinical.Psychology, Neuroscience, Kinesiology, Physiology, Psychology, Health.Education, Nursing and Behavior.Analysis. So it might represent the health-related programs. PC3 has stronger loadings for Design.and.Development.of.Digital.Games,Cognitive.Science, Learning.Analytics, Mathematics, Education.Technology, Creative.Technologies, Instructional.Technology.and.Media, Measurement.Evaluation.and.Statistics and Communication.Media.and.Learning.Technologies. PC3 is more related to math and technology or STEM fields. PC4 has higher loadings for Linguistics, English.Education, Teaching.English, Literacy, Deaf.and.Hard.of.Hearing. We can infer from the results that PC4 might be more related to language learning. + ``` diff --git a/assignment5.html b/assignment5.html new file mode 100644 index 0000000..988c431 --- /dev/null +++ b/assignment5.html @@ -0,0 +1,696 @@ + + + + + + + + + + + + + +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",header = TRUE)
+
+
+

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. 
+
+#I found that mean_correct is highly related to prior_percent_correct and mean_hint.
+
+
+

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 <- subset(D1, select = -(mean_correct))
+
+
+

Now run the PCA on the new data frame

+
pca <- prcomp(D2[ , 2:7], 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.

+

I would like to drop PC4, PC5, PC6, PC7 because the explained variation is less than 1 . From the plot, we can infer that the slope begins decreasing from PC4.

+
+
+

Part II

+
#Now, create a data frame of the transformed data from your pca.
+
+D3 <- data.frame(pca$x)
+
+#drop pc4, pc5 and pc6
+D3_2<-D3[, 1:3]
+D3_2<-data.frame(D3_2, D1$mean_correct)
+names(D3_2)[4]<-"mean_correct"
+
+#Attach the variable "mean_correct" from your original data frame to D3.
+
+D3 <- data.frame(D3, D1$mean_correct)
+names(D3)[7] <-"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)
+

+
ggpairs(D3_2,progress = FALSE)
+

+
#mean_correct is significantly correlated with PC1(r=.28, p<.001), PC2(r=.37, p<.001), PC4(r=.13, p<.05), and PC6(r=-.39, p<.001). If we drop PC4, PC5, and PC6, we would not be able to spot the associations bwtween mean_correct and PC4 & PC6
+
+
+

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
+
+#abs() will make all eigenvectors positive
+loadings <- abs(pca$rotation)
+
+#Now examine your components and try to come up with substantive descriptions of what some might represent?
+
+#PC 1 has a strong negative loading for mean_hint and mean attempt, and positive loadings for prior_percent_correct. These variables stand for the performance in current session. PC 2 is dominated by large positive loadings for prior_percent_correct and positive loadings for prior_prob_count. As these are analyses of students' working through online math problems which may reflect students’ prior experiences to the current session. PC 3 is dominated by large negative loadings for mean_confidence, so this might reflect the confidence of students in solving math problems. As for PC4 to PC 6, the factor loadings of these components don't show distinct patterns so it is challenging to tell what these components might represent. In addition, the contributed variance is not high. As a result, it is difficult to provide theoretical intepretations for these components.
+
+
+#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.

+
TC <- read.csv("tc-program-combos.csv", header = TRUE)
+TC <- TC[-c(69),]
+
+#pca
+pca_TC <- prcomp(TC[, 2:68], scale. = TRUE)
+plot(pca_TC, type = "line")
+

+
#Choose PC1 to PC4 as major components. Although we can see that the eigenvalues from PC1 to PC24 are larger than 1, we can still see from the plot where the slope starts to decrease from PC4. So I decide to choose PC1 to PC4.
+
+pca_TC$rotation[ ,1:4]
+
##                                                        PC1          PC2
+## Adult.Education                                0.194907620 -0.083660702
+## Anthropology                                   0.041360196  0.066809252
+## Social.Studies                                 0.117837722 -0.010380697
+## Physiology                                    -0.108687405  0.244542326
+## Behavior.Analysis                              0.073661207  0.211440119
+## Linguistics                                    0.030936146 -0.058129368
+## Art.Education                                 -0.101375553 -0.009569729
+## Teaching.English                               0.095633847 -0.080037587
+## Arts.Administration                            0.219060558 -0.009467423
+## Bilingual.Bicultural.Education                 0.186769330 -0.090013968
+## Clinical.Psychology                            0.137768455  0.252356427
+## Cognitive.Science                             -0.003918107  0.137624238
+## College.Advising                               0.136324871 -0.070307624
+## Communication.Sciences.and.Disorders           0.076814691  0.151509514
+## Cooperation.and.Conflict.Resolution            0.204359318  0.072236705
+## Creative.Technologies                         -0.033911621 -0.040467901
+## Curriculum.and.Teaching                        0.046003116 -0.145594420
+## Dance.Education                               -0.018044738  0.031049590
+## Deaf.and.Hard.of.Hearing                      -0.010396059  0.190060635
+## Design.and.Development.of.Digital.Games       -0.032589151 -0.074604779
+## Developmental.Psychology                       0.081965256  0.151561100
+## Diabetes.Education                            -0.018253473  0.190171034
+## Early.Childhood.Education                      0.108108109 -0.022038878
+## Early.Childhood.Special.Education              0.011567112  0.140539711
+## Economics.and.Education                        0.230528054 -0.046015056
+## Education.Technology                           0.079829536 -0.072886402
+## Education.Leadership                           0.164432407 -0.021728410
+## Education.Policy                               0.220770376 -0.051250478
+## Inclusive.Education                            0.112380969  0.077137421
+## English.Education                              0.057473723 -0.041394592
+## Change.Leadership                              0.278930565  0.026329974
+## Nursing                                       -0.003854528  0.212602854
+## Gifted.Education                               0.060635617  0.034850339
+## Health.Education                              -0.069862687  0.222003175
+## Higher.and.Postsecondary.Education             0.049524691 -0.087291131
+## History                                        0.185629885  0.006006837
+## Instructional.Technology.and.Media             0.015320724  0.036757032
+## Intellectual.Disability.Autism                -0.037790353  0.056188222
+## International.and.Comparative.Education        0.147908115 -0.136693075
+## Kinesiology                                   -0.073255268  0.251470260
+## Learning.Analytics                             0.051252718  0.024021348
+## Literacy                                       0.101560946 -0.055550280
+## Mathematics                                   -0.022974286  0.017848432
+## Measurement..Evaluation.and.Statistics         0.053783158  0.025116436
+## Motor.Learning.and.Control                    -0.067528081  0.065840399
+## Movement.Science                              -0.093522663  0.154922264
+## Music                                          0.020618416 -0.037701230
+## Neuroscience                                   0.050526637  0.252119919
+## Nutrition                                     -0.096385849  0.100764683
+## Leadership                                     0.200003803 -0.043532302
+## Philosophy                                     0.049996731 -0.034949302
+## Physical.Education                            -0.005433719  0.210486469
+## Politics                                       0.218057264 -0.092737673
+## Private.School.Leadership                      0.204088360 -0.045205016
+## Psychological.Counseling                       0.022067733  0.180650487
+## Psychology                                     0.132906972  0.222443155
+## Reading                                        0.141097198  0.007766273
+## School.Psychology                              0.090602812  0.072743648
+## Science.Education                              0.002684045 -0.040137732
+## Sexuality..Women.and.Gender.in.Psychology      0.138506287  0.190610919
+## Social.Organizational.Psychology               0.214808381  0.065416987
+## Sociology                                      0.139463117  0.172151333
+## Spirituality.Mind.Body                         0.121844511  0.178198807
+## School.Principals                              0.217035061 -0.025926133
+## Urban.Education                                0.197396920 -0.005648150
+## Counseling.Psychology                          0.070050239  0.194279060
+## Communication.Media.and.Learning.Technologies  0.055551641  0.029954147
+##                                                        PC3          PC4
+## Adult.Education                                0.023445347 -0.001779881
+## Anthropology                                  -0.016237112 -0.187213616
+## Social.Studies                                -0.059407968 -0.065470345
+## Physiology                                    -0.028047691  0.084633393
+## Behavior.Analysis                              0.064098874  0.066371199
+## Linguistics                                    0.103870137 -0.349415088
+## Art.Education                                  0.042578385 -0.033737983
+## Teaching.English                               0.051025590 -0.269483354
+## Arts.Administration                            0.114963460 -0.028871393
+## Bilingual.Bicultural.Education                -0.008177843 -0.184820488
+## Clinical.Psychology                           -0.065226181 -0.040323389
+## Cognitive.Science                              0.310049646 -0.049256125
+## College.Advising                              -0.092884496  0.102227070
+## Communication.Sciences.and.Disorders           0.057656692 -0.171412811
+## Cooperation.and.Conflict.Resolution            0.011824946  0.044503086
+## Creative.Technologies                          0.257902454 -0.047398824
+## Curriculum.and.Teaching                       -0.031482057 -0.141613230
+## Dance.Education                                0.142857775 -0.129675555
+## Deaf.and.Hard.of.Hearing                       0.010521014 -0.226471838
+## Design.and.Development.of.Digital.Games        0.311811509 -0.157136368
+## Developmental.Psychology                      -0.021652316  0.021750739
+## Diabetes.Education                            -0.065866033 -0.056306543
+## Early.Childhood.Education                      0.011078506 -0.127877610
+## Early.Childhood.Special.Education             -0.113006561 -0.165963173
+## Economics.and.Education                       -0.038509679  0.153925687
+## Education.Technology                           0.267265344  0.122994522
+## Education.Leadership                          -0.020704352  0.142784862
+## Education.Policy                              -0.065158499  0.071827191
+## Inclusive.Education                           -0.042312265  0.029020546
+## English.Education                              0.024145476 -0.337399324
+## Change.Leadership                              0.033050552  0.083651439
+## Nursing                                       -0.121033891 -0.008624699
+## Gifted.Education                               0.140880110 -0.009719421
+## Health.Education                              -0.146182194  0.040814576
+## Higher.and.Postsecondary.Education            -0.046200849  0.053065587
+## History                                       -0.027254571 -0.143212616
+## Instructional.Technology.and.Media             0.257463729 -0.039080666
+## Intellectual.Disability.Autism                -0.061585850 -0.104119711
+## International.and.Comparative.Education        0.009943997  0.068314251
+## Kinesiology                                    0.034908809  0.027182635
+## Learning.Analytics                             0.287201552  0.082142410
+## Literacy                                       0.017567595 -0.252038629
+## Mathematics                                    0.280551662  0.157390715
+## Measurement..Evaluation.and.Statistics         0.250418937  0.107224362
+## Motor.Learning.and.Control                     0.159382979  0.153013712
+## Movement.Science                               0.055769506  0.051741949
+## Music                                          0.122488030 -0.048894835
+## Neuroscience                                   0.127890318  0.017913724
+## Nutrition                                     -0.113547665 -0.066261396
+## Leadership                                    -0.087293702  0.178734295
+## Philosophy                                    -0.013583324  0.016072657
+## Physical.Education                             0.071997259  0.044136909
+## Politics                                      -0.046830268  0.113182579
+## Private.School.Leadership                      0.021496564  0.087300718
+## Psychological.Counseling                      -0.061950759 -0.125561810
+## Psychology                                     0.140185172  0.053162628
+## Reading                                        0.043409848 -0.167814085
+## School.Psychology                             -0.047740125 -0.028256768
+## Science.Education                              0.140867467 -0.011956768
+## Sexuality..Women.and.Gender.in.Psychology     -0.015739877  0.016023814
+## Social.Organizational.Psychology              -0.091221647 -0.075235719
+## Sociology                                     -0.084962825  0.132987962
+## Spirituality.Mind.Body                        -0.059166422 -0.106321742
+## School.Principals                             -0.044666191  0.007921960
+## Urban.Education                                0.006281815 -0.022259541
+## Counseling.Psychology                          0.061477795  0.013321688
+## Communication.Media.and.Learning.Technologies  0.224566731  0.055796280
+
#create a data frame for analysis
+PCAIII <- data.frame(abs(pca_TC$rotation[ ,1:4]))
+
+#From the results, we can see that PC1 has larger loadings for change_leadership, Economics.and.Education, Education.Policy, Arts.Administration, School.Principals, Social.Organizational.Psychology. So we can know that PC1 might stand for programs related to administration, school policy and leadership. PC2 has stronger loadings for Clinical.Psychology, Neuroscience, Kinesiology, Physiology, Psychology, Health.Education, Nursing and Behavior.Analysis. So it might represent the health-related programs. PC3 has stronger loadings for Design.and.Development.of.Digital.Games,Cognitive.Science, Learning.Analytics, Mathematics, Education.Technology, Creative.Technologies, Instructional.Technology.and.Media, Measurement.Evaluation.and.Statistics and Communication.Media.and.Learning.Technologies. PC3 is more related to math and technology or STEM fields. PC4 has higher loadings for Linguistics, English.Education, Teaching.English, Literacy, Deaf.and.Hard.of.Hearing. We can infer from the results that PC4 might be more related to language learning.
+
+ + + + +
+ + + + + + + + + + + + + + +