-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.R
38 lines (32 loc) · 1.05 KB
/
score.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#' Factor and component scores
#'
#' \code{score} calculates principal component or factor scores
#' based on the model generated by \code{\link{PCA}} and \code{\link{FA}}
#' functions
#'
#' @param data the data frame used to generate a PCA or FA model.
#' @param model the model generated by the \code{PCA} or \code{FA} functions.
#'
#' @details
#' The \code{score} function adds component or factor scores to a data frame. Scores
#' are calculated using the regression method.
#'
#' @export
#'
#' @seealso \link{PCA}, \link{FA}.
#'
#' @return the data frame with component or factor scores appended.
#' @examples
#' ratings <- read.csv("https://www.promptcloud.com/wp-content/uploads/2017/02/EFA.csv")
#' fit.pca <- PCA(ratings, nfactors=2, rotate="varimax")
#' newdf <- score(ratings, fit.pca)
#' head(newdf)
score <- function(data, model) {
# default number of factors
if (!inherits(model, "factorAnalysis")){
stop("model in not of class 'factorAnalysis', call. = FALSE")
}
scores <- as.data.frame(model$scores)
df <- cbind(data, scores)
invisible(df)
}