Skip to content

Commit

Permalink
Merge pull request #65 from neuropsychology/dev
Browse files Browse the repository at this point in the history
prepare CRAN
  • Loading branch information
DominiqueMakowski authored Jun 11, 2018
2 parents b62e316 + d5a3db5 commit d74dd5a
Show file tree
Hide file tree
Showing 38 changed files with 923 additions and 176 deletions.
3 changes: 1 addition & 2 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Contribution Guidelines
**All people are very much welcome to contribute to code, documentation, testing and suggestions.**


`psycho` is a beginner-friendly package. Even if you're new to all this open-source way of life, new to coding and submitting pull requests (PRs), we encourage you to try. You can submit some recommendations in the [issues](https://github.com/neuropsychology/psycho.R/issues) or start coding a function or adding some parameters, fixing a bug or just rewriting a bit so it is more clear, more documented or understandable. Then, do not hesitate to sumbit your changes. We will work together to integrate them :)

## Code
- Authors of code contribution will be added within the [**authors**](https://github.com/neuropsychology/psycho.R/blob/master/DESCRIPTION) section within the package description.
- Please document and comment your code, so that the purpose of each step (or code line) is stated in a clear and understandable way.
- Avoid unnecessary function splitting into smaller bits: it makes testing and reading of the code more difficult (as one must jump between functions and files to understand what's happening).
- Before submitting a change, please use [**styler**](https://github.com/r-lib/styler) to nicely format your code.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: psycho
Type: Package
Title: Efficient and Publishing-Oriented Workflow for Psychological Science
Version: 0.2.7
Version: 0.2.8
Authors@R: c(
person("Dominique",
"Makowski",
Expand Down Expand Up @@ -54,7 +54,8 @@ Imports:
pbkrtest,
BayesFactor (>= 0.9.1),
scales,
loo (>= 2.0.0)
loo (>= 2.0.0),
lavaan
Suggests:
knitr,
rmarkdown,
Expand Down
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Generated by roxygen2: do not edit by hand

S3method(analyze,aov)
S3method(analyze,fa)
S3method(analyze,glmerMod)
S3method(analyze,htest)
S3method(analyze,lavaan)
S3method(analyze,lm)
S3method(analyze,lmerModLmerTest)
S3method(analyze,stanreg)
Expand Down Expand Up @@ -47,6 +49,7 @@ export(get_std_posteriors)
export(hdi)
export(interpret_R2)
export(interpret_R2_posterior)
export(interpret_RMSEA)
export(interpret_bf)
export(interpret_d)
export(interpret_d_posterior)
Expand All @@ -67,11 +70,13 @@ export(plot_loadings)
export(power_analysis)
export(probs_to_odds)
export(refdata)
export(remove_empty_cols)
export(reorder_matrix)
export(rnorm_perfect)
export(rope)
export(standardize)
export(values)
import(broom)
import(coda)
import(dplyr)
import(ggcorrplot)
Expand All @@ -98,6 +103,8 @@ importFrom(ggplot2,cut_number)
importFrom(ggplot2,element_text)
importFrom(ggplot2,theme)
importFrom(graphics,pairs)
importFrom(lavaan,fitmeasures)
importFrom(lavaan,parameterEstimates)
importFrom(nFactors,moreStats)
importFrom(nFactors,nScree)
importFrom(psych,VSS)
Expand Down Expand Up @@ -146,6 +153,7 @@ importFrom(stringr,str_replace)
importFrom(stringr,str_squish)
importFrom(stringr,str_to_title)
importFrom(tibble,rownames_to_column)
importFrom(utils,capture.output)
importFrom(utils,combn)
importFrom(utils,head)
importFrom(utils,tail)
7 changes: 5 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### Changes


# [0.2.7](https://github.com/neuropsychology/psycho.R/releases/tag/0.2.0) (2018-05-11)
# [0.2.8](https://github.com/neuropsychology/psycho.R/releases/tag/0.2.8) (2018-06-11)


### Breaking changes
Expand All @@ -15,6 +15,9 @@
- effsize in analyze.stanreg default to TRUE
- Fixed bug in `dprime`
### New functions / parameters
- Added `interpret_RMSEA`
- Added `analyze.lavaan` and `analyze.aov`
- Added `remove_empty_cols`
- Added `model_to_priors`
- First iteration on custom rule's sets for effects sizes
- Added `analyze.htest` for correlations and t-tests
Expand Down Expand Up @@ -136,7 +139,7 @@
### New functions / parameters
### Major changes
### Minor changes
- Added [`CONTRIBUTING.md`](https://github.com/neuropsychology/psycho.R/blob/master/CONTRIBUTING.md)
- Added `CONTRIBUTING.md`
- Changed `format_digit`
- Added `except` parameter to `normalize`

Expand Down
4 changes: 4 additions & 0 deletions R/analyze.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
#' \item{\link[=analyze.lmerModLmerTest]{analyze.merModLmerTest}}
#' \item{\link[=analyze.glmerMod]{analyze.glmerMod}}
#' \item{\link[=analyze.lm]{analyze.lm}}
#' }
#' #' \itemize{
#' \item{\link[=analyze.htest]{analyze.htest}}
#' \item{\link[=analyze.aov]{analyze.aov}}
#' }
#' \itemize{
#' \item{\link[=analyze.fa]{analyze.fa}}
#' \item{\link[=analyze.fa]{analyze.lavaan}}
#' }
#'
#' @param x object to analyze.
Expand Down
50 changes: 50 additions & 0 deletions R/analyze.aov.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#' Analyze aov objects.
#'
#' Analyze aov objects.
#'
#' @param x aov object.
#' @param ... Arguments passed to or from other methods.
#'
#' @return output
#'
#' @examples
#' library(psycho)
#'
#' df <- psycho::affective
#'
#' x <- aov(df$Tolerating ~ df$Sex)
#'
#' summary(analyze(x))
#'
#'
#' @author \href{https://dominiquemakowski.github.io/}{Dominique Makowski}
#'
#' @import broom
#'
#' @export
analyze.aov <- function(x, ...) {

# TODO: this must be enhanced!

# Processing
# -------------
values <- broom::tidy(x)

# Text
# -------------
text <- "Not available yet"


# Summary
# -------------
summary <- values

# Plot
# -------------
plot <- "Not available yet"

output <- list(text = text, plot = plot, summary = summary, values = values)

class(output) <- c("psychobject", "list")
return(output)
}
56 changes: 35 additions & 21 deletions R/analyze.fa.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,36 @@ analyze.fa <- function(x, labels=NULL, treshold="max", ...) {
variance <- as.data.frame(variance)
n_factors <- ncol(variance)

t <- as.data.frame(t(variance))
tot_var <- max(t$`Cumulative Var`)

factors <- names(variance)
var <- variance["Proportion Var", ]
text_var <- paste0(factors,
" = ",
format_digit(var * 100),
"%",
collapse = ", "
)

text <- paste0(
"The ",
n_factors,
" components accounted for ",
format_digit(tot_var * 100),
"% of the total variance ("
)
text <- paste0(text, text_var, ")")
if (ncol(variance) == 1) {
t <- as.data.frame(t(variance))
tot_var <- t$`Proportion Var`
text <- paste0(
"The unique component accounted for ",
format_digit(tot_var * 100),
"% of the total variance."
)
} else {
t <- as.data.frame(t(variance))
tot_var <- max(t$`Cumulative Var`)

factors <- names(variance)
var <- variance["Proportion Var", ]
text_var <- paste0(factors,
" = ",
format_digit(var * 100),
"%",
collapse = ", "
)

text <- paste0(
"The ",
n_factors,
" components accounted for ",
format_digit(tot_var * 100),
"% of the total variance ("
)
text <- paste0(text, text_var, ").")
}

return(text)
}
Expand Down Expand Up @@ -239,10 +249,14 @@ get_cfa_model <- function(loadings, treshold="max") {
#' @import dplyr
#' @export
plot_loadings <- function(loadings) {
if (all(loadings$Label != loadings$N)) {
loadings$Item <- paste0(loadings$Label, " (", loadings$Item, ")")
}

p <- loadings %>%
gather("Component", "Loading", matches("\\d$")) %>%
mutate_("Loading" = "abs(Loading)") %>%
mutate_("Item" = "factor(Item, levels=get_loadings_max(loadings)$Item)") %>%
mutate_("Item" = "factor(Item, levels=rev(get_loadings_max(loadings)$Item))") %>%
ggplot(aes_string(y = "Loading", x = "Item", fill = "Component")) +
geom_bar(stat = "identity") +
coord_flip() +
Expand Down
142 changes: 142 additions & 0 deletions R/analyze.lavaan.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#' Analyze aov objects.
#'
#' Analyze aov objects.
#'
#' @param x aov object.
#' @param ... Arguments passed to or from other methods.
#'
#' @return output
#'
#' @examples
#' library(psycho)
#' library(lavaan)
#'
#' model <- ' visual =~ x1 + x2 + x3
#' textual =~ x4 + x5 + x6
#' speed =~ x7 + x8 + x9 '
#' x <- lavaan::cfa(model, data=HolzingerSwineford1939)
#'
#' rez <- analyze(x)
#' print(rez)
#'
#'
#' @author \href{https://dominiquemakowski.github.io/}{Dominique Makowski}
#'
#' @seealso
#' https://www.researchgate.net/post/Whats_the_standard_of_fit_indices_in_SEM
#'
#'
#' @importFrom lavaan parameterEstimates fitmeasures
#'
#' @export
analyze.lavaan <- function(x, ...) {

# TODO: this must be enhanced!

# Processing
# -------------

# TODO: tWait for broom to implement methods for lavaan objects!
values <- list()

indices <- lavaan::fitmeasures(x)
for (index in names(indices)) {
values[index] <- indices[index]
}

# awang2012
# https://www.researchgate.net/post/Whats_the_standard_of_fit_indices_in_SEM
if (values$cfi >= 0.9) {
cfi <- "satisfactory"
} else {
cfi <- "poor"
}
if (values$rmsea <= 0.08) {
rmsea <- "satisfactory"
} else {
rmsea <- "poor"
}
if (values$gfi >= 0.9) {
gfi <- "satisfactory"
} else {
gfi <- "poor"
}
if (values$tli >= 0.9) {
tli <- "satisfactory"
} else {
tli <- "poor"
}
if (values$nfi >= 0.9) {
nfi <- "satisfactory"
} else {
nfi <- "poor"
}
fit <- data.frame(
Index = c("RMSEA", "CFI", "GFI", "TLI", "NFI", "Chisq"),
Value = c(values$rmsea, values$cfi, values$gfi, values$tli, values$nfi, values$chisq),
Interpretation = c(rmsea, cfi, gfi, tli, nfi, NA),
Treshold = c("< .08", "> .9", "> .9", "> .9", "> .9", NA)
)




# Text
# -------------
if ("satisfactory" %in% fit$Interpretation) {
satisfactory <- fit %>%
filter_("Interpretation == 'satisfactory'") %>%
mutate_("Index" = "paste0(Index, ' (', format_digit(Value), ' ', Treshold, ')')") %>%
select_("Index") %>%
pull() %>%
paste0(collapse = ", ")
satisfactory <- paste0("The ", satisfactory, " show satisfactory indices of fit.")
} else {
satisfactory <- ""
}
if ("poor" %in% fit$Interpretation) {
poor <- fit %>%
filter_("Interpretation == 'poor'") %>%
mutate_(
"Treshold" = 'stringr::str_replace(Treshold, "<", "SUP")',
"Treshold" = 'stringr::str_replace(Treshold, ">", "INF")',
"Treshold" = 'stringr::str_replace(Treshold, "SUP", ">")',
"Treshold" = 'stringr::str_replace(Treshold, "INF", "<")'
) %>%
mutate_("Index" = "paste0(Index, ' (', format_digit(Value), ' ', Treshold, ')')") %>%
select_("Index") %>%
pull() %>%
paste0(collapse = ", ")
poor <- paste0("The ", poor, " show poor indices of fit.")
} else {
poor <- ""
}
text <- paste(satisfactory, poor)


# Summary
# -------------
summary <- lavaan::parameterEstimates(x) %>%
as_data_frame() %>%
tibble::rownames_to_column() %>%
mutate_("term" = "paste(lhs, op, rhs)") %>%
rename(
"estimate" = "est",
"Operator" = "op",
"std.error" = "se",
"p.value" = "pvalue",
"statistic" = "z",
"CI_lower" = "ci.lower",
"CI_upper" = "ci.upper"
) %>%
select_("term", "Operator", "everything()", "-rowname", "-lhs", "-rhs")

# Plot
# -------------
plot <- "Not available yet"

output <- list(text = text, plot = plot, summary = summary, values = values)

class(output) <- c("psychobject", "list")
return(output)
}
Loading

0 comments on commit d74dd5a

Please sign in to comment.