Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Factor Scores to the Original Dataset

Tags:

r

I run the factor analysis and generated 5 factors. Now, I want to add these factors in the original dataset to run regression keeping these factors using independent variables. Can anybody please tell me how how to do it? The code I used for factor analysis is the following:

result.1<-subset(result,select=c(17:27))
fa.parallel(result.1)
View(result.1)
result.2<-factanal(result.1,factors=5,rotation="promax")
print(result.2)
print(result.2, digits = 2, cutoff = .2, sort = TRUE)
colnames(result.2$loadings)<-c("Fac_1","Fac_2","Fac_3","Fac_4","Fac_5")
print(loadings(result.2), digits = 2, cutoff = .2, sort = TRUE)

I tried to use cbind to get the new variable columns of factors, but unfortunately it didn't work.

result.fac<-cbind(result,result.2)

Regards, Ari

like image 997
Beta Avatar asked Nov 24 '25 23:11

Beta


2 Answers

You have to save the scores computed by factanal and cbind those to the original dataset. E.g.:

data <- mtcars
f <- factanal(data, factors=5, rotation="promax", scores="regression")
data <- cbind(data, f$scores)
like image 62
daroczig Avatar answered Nov 27 '25 12:11

daroczig


You probably have some missing data which results in missing rows in the factor scores matrix. You need to match on rownames, like this:

scores <- result.2$scores
result.fac <- cbind(result[as.integer(rownames(scores)),],scores)
like image 30
enquiry Avatar answered Nov 27 '25 14:11

enquiry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!