Skip to content

Commit

Permalink
make lintr a bit happier
Browse files Browse the repository at this point in the history
  • Loading branch information
DominiqueMakowski committed Nov 11, 2017
1 parent 716169a commit e5366c3
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions R/n_factors.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@
n_factors <- function(df, rotate="varimax", fm="minres", n_max=8){

# Copy the parallel function from nFactors to correct the use of mvrnorm
parallel <- function (subject = 100, var = 10, rep = 100, cent = 0.05, quantile = cent,
model = "components", sd = diag(1, var), ...)
parallel <- function (subject = 100, var = 10, rep = 100, cent = 0.05,
quantile = cent, model = "components",
sd = diag(1, var), ...)
{
r <- subject
c <- var
y <- matrix(c(1:r * c), nrow = r, ncol = c)
ycor <- matrix(c(1:c * c), nrow = c, ncol = c)
evpea <- NULL
leg.txt <- "Pearson"
for (k in c(1:rep)) {
y <- MASS::mvrnorm(n = r, mu = rep(0, var), Sigma = sd, empirical = FALSE)
corY <- cov(y, ...)
Expand All @@ -57,15 +56,17 @@ n_factors <- function(df, rotate="varimax", fm="minres", n_max=8){
SEcentile <- function(sd, n = 100, p = 0.95) {
return(sd/sqrt(n) * sqrt(p * (1 - p))/dnorm(qnorm(p)))
}
sprob <- c(cent)
mevpea <- sapply(as.data.frame(evpea), mean)
sevpea <- sapply(as.data.frame(evpea), sd)
qevpea <- nFactors::moreStats(evpea, quantile = quantile)[3, ]
sqevpea <- sevpea
sqevpea <- sapply(as.data.frame(sqevpea), SEcentile, n = rep,
p = cent)
result <- list(eigen = data.frame(mevpea, sevpea, qevpea,
sqevpea), subject = r, variables = c, centile = cent)
sqevpea),
subject = r,
variables = c,
centile = cent)
class(result) <- "parallel"
return(result)
}
Expand All @@ -86,10 +87,18 @@ n_factors <- function(df, rotate="varimax", fm="minres", n_max=8){

# Processing
# -------------------
results <- data.frame(Method=c("Optimal Coordinates", "Acceleration Factor", "Parallel Analysis", "Eigenvalues (Kaiser Criterion)"), n_optimal=as.numeric(nS$Components[1,]))


vss <- psych::VSS(cor, n=n_max, n.obs=nrow(df), rotate=rotate, fm=fm, plot=F) # fm can be "pa", "pc", "minres", "mle"
results <- data.frame(Method=c("Optimal Coordinates",
"Acceleration Factor",
"Parallel Analysis",
"Eigenvalues (Kaiser Criterion)"),
n_optimal=as.numeric(nS$Components[1,]))


vss <- psych::VSS(cor,
n=n_max,
n.obs=nrow(df),
rotate=rotate,
fm=fm, plot=F) # fm can be "pa", "pc", "minres", "mle"
stats <- vss$vss.stats
stats$map <- vss$map
stats$n_factors <- 1:nrow(stats)
Expand Down

1 comment on commit e5366c3

@lintr-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R/n_factors.R:57:16: style: Put spaces around all infix operators.

return(sd/sqrt(n) * sqrt(p * (1 - p))/dnorm(qnorm(p)))
              ~^~

R/n_factors.R:57:44: style: Put spaces around all infix operators.

return(sd/sqrt(n) * sqrt(p * (1 - p))/dnorm(qnorm(p)))
                                          ~^~

R/n_factors.R:77:25: style: Put spaces around all infix operators.

ap <- parallel(subject=nrow(df), var=ncol(df))
                       ~^~

R/n_factors.R:77:39: style: Put spaces around all infix operators.

ap <- parallel(subject=nrow(df), var=ncol(df))
                                     ~^~

R/n_factors.R:78:27: style: Put spaces around all infix operators.

nS <- nFactors::nScree(x=eigen(cor)$values, aparallel=ap$eigen$qevpea)
                         ~^~

R/n_factors.R:78:56: style: Put spaces around all infix operators.

nS <- nFactors::nScree(x=eigen(cor)$values, aparallel=ap$eigen$qevpea)
                                                      ~^~

R/n_factors.R:83:33: style: Put spaces around all infix operators.

"Exp.Variance"="Prop",
                               ~^~

R/n_factors.R:84:33: style: Put spaces around all infix operators.

"Cum.Variance"="Cumu") %>%
                               ~^~

R/n_factors.R:85:24: style: Put spaces around all infix operators.

​    mutate_("n.Factors"= ~1:nrow(nS$Analysis))
                      ~^

R/n_factors.R:90:31: style: Put spaces around all infix operators.

results <- data.frame(Method=c("Optimal Coordinates",
                             ~^~

R/n_factors.R:94:34: style: Put spaces around all infix operators.

n_optimal=as.numeric(nS$Components[1,]))
                                ~^~

R/n_factors.R:98:22: style: Put spaces around all infix operators.

n=n_max,
                    ~^~

R/n_factors.R:99:26: style: Put spaces around all infix operators.

n.obs=nrow(df),
                        ~^~

R/n_factors.R:100:27: style: Put spaces around all infix operators.

rotate=rotate,
                         ~^~

R/n_factors.R:101:23: style: Put spaces around all infix operators.

fm=fm, plot=F)  # fm can be "pa", "pc", "minres", "mle"
                     ~^~

R/n_factors.R:101:32: style: Put spaces around all infix operators.

fm=fm, plot=F)  # fm can be "pa", "pc", "minres", "mle"
                              ~^~

R/n_factors.R:106:32: style: Put spaces around all infix operators.

results2 <- data.frame(Method=c("Velicer MAP",
                              ~^~

R/n_factors.R:109:1: style: lines should not be more than 80 characters.

n_optimal=c(na.omit(stats[stats$map==min(stats$map, na.rm = T),])$n_factors,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

R/n_factors.R:109:35: style: Put spaces around all infix operators.

n_optimal=c(na.omit(stats[stats$map==min(stats$map, na.rm = T),])$n_factors,
                                 ~^~

R/n_factors.R:109:61: style: Put spaces around all infix operators.

n_optimal=c(na.omit(stats[stats$map==min(stats$map, na.rm = T),])$n_factors,
                                                           ~^~~

R/n_factors.R:110:1: style: lines should not be more than 80 characters.

​                                     na.omit(stats[stats$BIC==min(stats$BIC, na.rm = T),])$n_factors,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

R/n_factors.R:110:61: style: Put spaces around all infix operators.

​                                     na.omit(stats[stats$BIC==min(stats$BIC, na.rm = T),])$n_factors,
                                                           ~^~~

R/n_factors.R:111:1: style: lines should not be more than 80 characters.

​                                     na.omit(stats[stats$SABIC==min(stats$SABIC, na.rm = T),])$n_factors))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

R/n_factors.R:111:63: style: Put spaces around all infix operators.

​                                     na.omit(stats[stats$SABIC==min(stats$SABIC, na.rm = T),])$n_factors))
                                                             ~^~~

R/n_factors.R:119:28: style: Put spaces around all infix operators.

cfit <- data.frame(cfit=cfit, n_factors=1:length(cfit))
                          ~^~

R/n_factors.R:119:44: style: Put spaces around all infix operators.

cfit <- data.frame(cfit=cfit, n_factors=1:length(cfit))
                                          ~^~

R/n_factors.R:121:33: style: Put spaces around all infix operators.

result3 <- data.frame(Method=c(gsub("cfit.", "VSS Complexity ", name)),
                               ~^~

R/n_factors.R:122:1: style: lines should not be more than 80 characters.

n_optimal=c(na.omit(cfit[cfit$cfit==max(cfit$cfit, na.rm = T),])$n_factors))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

R/n_factors.R:122:36: style: Put spaces around all infix operators.

n_optimal=c(na.omit(cfit[cfit$cfit==max(cfit$cfit, na.rm = T),])$n_factors))
                                  ~^~

R/n_factors.R:122:61: style: Put spaces around all infix operators.

n_optimal=c(na.omit(cfit[cfit$cfit==max(cfit$cfit, na.rm = T),])$n_factors))
                                                           ~^~~

R/n_factors.R:130:26: style: Put spaces around all infix operators.

​    summarise_("n_method"=~n()) %>%
                        ~^~

R/n_factors.R:131:24: style: Put spaces around all infix operators.

​    mutate_("n_optimal"=~factor(n_optimal, levels=1:nrow(eigenvalues))) %>%
                      ~^~

R/n_factors.R:131:50: style: Put spaces around all infix operators.

​    mutate_("n_optimal"=~factor(n_optimal, levels=1:nrow(eigenvalues))) %>%
                                                ~^~

R/n_factors.R:132:32: style: Put spaces around all infix operators.

​    complete_("n_optimal", fill=list(n_method = 0)) %>%
                              ~^~

R/n_factors.R:134:24: style: Put spaces around all infix operators.

​    rename_("n.Factors"="n_optimal",
                      ~^~

R/n_factors.R:135:23: style: Put spaces around all infix operators.

"n.Methods"="n_method") %>%
                     ~^~

R/n_factors.R:136:24: style: Put spaces around all infix operators.

​    mutate_("n.Factors"=~as.integer(n.Factors)) %>%
                      ~^~

R/n_factors.R:137:30: style: Put spaces around all infix operators.

​    left_join(eigenvalues, by="n.Factors")
                            ~^~

R/n_factors.R:142:29: style: Put spaces around all infix operators.

values <- list(eigenvalues=eigenvalues, methods=results)
                           ~^~

R/n_factors.R:142:50: style: Put spaces around all infix operators.

values <- list(eigenvalues=eigenvalues, methods=results)
                                                ~^~

R/n_factors.R:156:51: style: Put spaces around all infix operators.

plot_data$n.Methods.Ratio <- plot_data$n.Methods/sum(plot_data$n.Methods)
                                                 ~^~

R/n_factors.R:157:1: style: lines should not be more than 80 characters.

plot_data$n.Methods.Ratio <- plot_data$n.Methods.Ratio*(1/max(plot_data$n.Methods.Ratio))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

R/n_factors.R:157:57: style: Put spaces around all infix operators.

plot_data$n.Methods.Ratio <- plot_data$n.Methods.Ratio*(1/max(plot_data$n.Methods.Ratio))
                                                       ~^~

R/n_factors.R:157:60: style: Put spaces around all infix operators.

plot_data$n.Methods.Ratio <- plot_data$n.Methods.Ratio*(1/max(plot_data$n.Methods.Ratio))
                                                          ~^~

R/n_factors.R:158:1: style: lines should not be more than 80 characters.

plot_data$area <- plot_data$n.Methods.Ratio/(max(plot_data$n.Methods.Ratio) / max(plot_data$Eigenvalues))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

R/n_factors.R:158:46: style: Put spaces around all infix operators.

plot_data$area <- plot_data$n.Methods.Ratio/(max(plot_data$n.Methods.Ratio) / max(plot_data$Eigenvalues))
                                            ~^~

R/n_factors.R:159:1: style: lines should not be more than 80 characters.

plot_data$var <- plot_data$Cum.Variance/(max(plot_data$Cum.Variance) / max(plot_data$Eigenvalues))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

R/n_factors.R:159:42: style: Put spaces around all infix operators.

plot_data$var <- plot_data$Cum.Variance/(max(plot_data$Cum.Variance) / max(plot_data$Eigenvalues))
                                        ~^~

R/n_factors.R:162:24: style: Put spaces around all infix operators.

​    ggplot(aes_string(x="n.Factors", y="Eigenvalues")) +
                      ~^~

R/n_factors.R:162:39: style: Put spaces around all infix operators.

​    ggplot(aes_string(x="n.Factors", y="Eigenvalues")) +
                                     ~^~

R/n_factors.R:163:27: style: Put spaces around all infix operators.

​    geom_area(aes_string(y="area"),
                         ~^~

R/n_factors.R:164:19: style: Put spaces around all infix operators.

fill="#FFC107",
                 ~^~

R/n_factors.R:165:20: style: Put spaces around all infix operators.

alpha=0.5) +
                  ~^~

R/n_factors.R:166:21: style: Put spaces around all infix operators.

​    geom_line(colour="#E91E63",
                   ~^~

R/n_factors.R:167:19: style: Put spaces around all infix operators.

size=1) +
                 ~^~

R/n_factors.R:168:40: style: Put spaces around all infix operators.

​    geom_hline(yintercept = 1, linetype="dashed", colour="#607D8B") +
                                      ~^~

R/n_factors.R:168:57: style: Put spaces around all infix operators.

​    geom_hline(yintercept = 1, linetype="dashed", colour="#607D8B") +
                                                       ~^~

R/n_factors.R:170:21: style: Put spaces around all infix operators.

colour="#2196F3",
                   ~^~

R/n_factors.R:171:19: style: Put spaces around all infix operators.

size=1) +
                 ~^~

R/n_factors.R:172:1: style: lines should not be more than 80 characters.

​    scale_y_continuous(sec.axis = sec_axis(trans= ~.*(max(eigenvalues$Cum.Variance) / max(eigenvalues$Eigenvalues)),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

R/n_factors.R:172:49: style: Put spaces around all infix operators.

​    scale_y_continuous(sec.axis = sec_axis(trans= ~.*(max(eigenvalues$Cum.Variance) / max(eigenvalues$Eigenvalues)),
                                               ~^

R/n_factors.R:172:53: style: Put spaces around all infix operators.

​    scale_y_continuous(sec.axis = sec_axis(trans= ~.*(max(eigenvalues$Cum.Variance) / max(eigenvalues$Eigenvalues)),
                                                   ~^~

R/n_factors.R:173:51: style: Only use double-quotes.

name = 'Cumulative Variance\n')) +
                                                  ^~~~~~~~~~~~~~~~~~~~~~~

Please sign in to comment.