###Code for lecture 34 - Factor Analysis ### Import Data datum=read.csv(file.choose()) ### exploratory factor analysis is done with the poorly name function 'factanal' help(factanal) # note that requires the argument 'factors', which is how many factors your think there are results2=factanal(datum,factors=2) ### lets examine what we know is truth results2 ### Uniqueness is how much each variable is unlike other variables (close to 1 = very unique) ### close to zero is not very unique (heavily correlated with other variables) ###Proportion variance and cumulative variance explain just what they mean ### Beware of results in which the cum variance is low ###All loadings are high, and clearly group each variable into a single factor ### What happens if we don't specify enough factors results1=factanal(datum,factors=1) #### only one factor results1 ### Note at the very bottom, the p-value that is significant, indicates a rejection of the null hypothesis ### that 1 factor is sufficient to explain the data ### what happens if we specify too many factors results3=factanal(datum,factors=3) ### 3 factors results3 ### note third factor doesn't explain hardly any variation ### degrees of freedom for p-test is 0 - model is over-fit ### Scores for the latent variables are not produced by default ### if you want scores, then you need to include the argument: results2=factanal(datum,factors=2,scores="regression") results2$scores ###should give you the values of the latent variables ### Try it again, but with correlation between two latent variables datum=read.csv(file.choose()) results2=factanal(datum,factors=2) ### lets examine what we know is truth results2 ### still works, but loadings aren't as clear-cut results1=factanal(datum,factors=1) #### only one factor results1 ### Note at the very bottom, the p-value that is significant, indicates a rejection of the null hypothesis ### that 1 factor is sufficient to explain the data results3=factanal(datum,factors=3) ### 3 factors results3 ### note third factor doesn't explain hardly any variation ### degrees of freedom for p-test is 0 - model is over-fit