R/endoj.h.R

Defines functions endoj

# This file is automatically generated, you probably don't want to edit this

endojOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "endojOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            .caller = "lm",
            .interface = "jamovi",
            dep = NULL,
            exo = NULL,
            endo = NULL,
            iv = NULL,
            es = list(
                "beta"),
            estimates_ci = TRUE,
            betas_ci = FALSE,
            ci_width = 95,
            ci_method = "wald",
            se_method = "wald",
            boot_r = 1000,
            vcov = FALSE, ...) {

            super$initialize(
                package="endoj",
                name="endoj",
                requiresData=TRUE,
                ...)

            private$...caller <- jmvcore::OptionString$new(
                ".caller",
                .caller,
                default="lm",
                hidden=TRUE)
            private$...interface <- jmvcore::OptionString$new(
                ".interface",
                .interface,
                default="jamovi",
                hidden=TRUE)
            private$..dep <- jmvcore::OptionVariable$new(
                "dep",
                dep,
                default=NULL,
                permitted=list(
                    "factor",
                    "numeric"))
            private$..exo <- jmvcore::OptionVariables$new(
                "exo",
                exo,
                suggested=list(
                    "continuous",
                    "ordinal"),
                permitted=list(
                    "numeric"),
                default=NULL)
            private$..endo <- jmvcore::OptionVariables$new(
                "endo",
                endo,
                suggested=list(
                    "continuous",
                    "ordinal"),
                permitted=list(
                    "numeric"),
                default=NULL)
            private$..iv <- jmvcore::OptionVariables$new(
                "iv",
                iv,
                suggested=list(
                    "continuous",
                    "ordinal"),
                permitted=list(
                    "numeric"),
                default=NULL)
            private$..es <- jmvcore::OptionNMXList$new(
                "es",
                es,
                options=list(
                    "beta"),
                default=list(
                    "beta"))
            private$..estimates_ci <- jmvcore::OptionBool$new(
                "estimates_ci",
                estimates_ci,
                default=TRUE)
            private$..betas_ci <- jmvcore::OptionBool$new(
                "betas_ci",
                betas_ci,
                default=FALSE)
            private$..ci_width <- jmvcore::OptionNumber$new(
                "ci_width",
                ci_width,
                min=50,
                max=99.9,
                default=95)
            private$..ci_method <- jmvcore::OptionList$new(
                "ci_method",
                ci_method,
                default="wald",
                options=list(
                    "wald",
                    "perc",
                    "bca"))
            private$..se_method <- jmvcore::OptionList$new(
                "se_method",
                se_method,
                default="wald",
                options=list(
                    "wald",
                    "robust"))
            private$..boot_r <- jmvcore::OptionNumber$new(
                "boot_r",
                boot_r,
                min=1,
                default=1000)
            private$..vcov <- jmvcore::OptionBool$new(
                "vcov",
                vcov,
                default=FALSE)
            private$..residuals <- jmvcore::OptionOutput$new(
                "residuals")

            self$.addOption(private$...caller)
            self$.addOption(private$...interface)
            self$.addOption(private$..dep)
            self$.addOption(private$..exo)
            self$.addOption(private$..endo)
            self$.addOption(private$..iv)
            self$.addOption(private$..es)
            self$.addOption(private$..estimates_ci)
            self$.addOption(private$..betas_ci)
            self$.addOption(private$..ci_width)
            self$.addOption(private$..ci_method)
            self$.addOption(private$..se_method)
            self$.addOption(private$..boot_r)
            self$.addOption(private$..vcov)
            self$.addOption(private$..residuals)
        }),
    active = list(
        .caller = function() private$...caller$value,
        .interface = function() private$...interface$value,
        dep = function() private$..dep$value,
        exo = function() private$..exo$value,
        endo = function() private$..endo$value,
        iv = function() private$..iv$value,
        es = function() private$..es$value,
        estimates_ci = function() private$..estimates_ci$value,
        betas_ci = function() private$..betas_ci$value,
        ci_width = function() private$..ci_width$value,
        ci_method = function() private$..ci_method$value,
        se_method = function() private$..se_method$value,
        boot_r = function() private$..boot_r$value,
        vcov = function() private$..vcov$value,
        residuals = function() private$..residuals$value),
    private = list(
        ...caller = NA,
        ...interface = NA,
        ..dep = NA,
        ..exo = NA,
        ..endo = NA,
        ..iv = NA,
        ..es = NA,
        ..estimates_ci = NA,
        ..betas_ci = NA,
        ..ci_width = NA,
        ..ci_method = NA,
        ..se_method = NA,
        ..boot_r = NA,
        ..vcov = NA,
        ..residuals = NA)
)

endojResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "endojResults",
    inherit = jmvcore::Group,
    active = list(
        info = function() private$.items[["info"]],
        main = function() private$.items[["main"]],
        diagnostics = function() private$.items[["diagnostics"]],
        residuals = function() private$.items[["residuals"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Instrumental Variables Linear Models")
            self$add(jmvcore::Table$new(
                options=options,
                name="info",
                title="Some Info",
                columns=list(
                    list(
                        `name`="info", 
                        `type`="text", 
                        `title`=""),
                    list(
                        `name`="specs", 
                        `type`="text", 
                        `title`="")),
                clearWith=list(
                    "dep")))
            self$add(R6::R6Class(
                inherit = jmvcore::Group,
                active = list(
                    r2 = function() private$.items[["r2"]],
                    anova = function() private$.items[["anova"]],
                    coefficients = function() private$.items[["coefficients"]],
                    vcov = function() private$.items[["vcov"]]),
                private = list(),
                public=list(
                    initialize=function(options) {
                        super$initialize(
                            options=options,
                            name="main",
                            title="Model Results")
                        self$add(jmvcore::Table$new(
                            options=options,
                            name="r2",
                            title="Model Fit",
                            clearWith=list(
                                "dep",
                                "endo",
                                "exo",
                                "iv"),
                            columns=list(
                                list(
                                    `name`="r2", 
                                    `title`="R\u00B2", 
                                    `type`="number"),
                                list(
                                    `name`="ar2", 
                                    `title`="Adj. R\u00B2", 
                                    `type`="number"),
                                list(
                                    `name`="df1", 
                                    `title`="df", 
                                    `type`="integer"),
                                list(
                                    `name`="df2", 
                                    `title`="df (res)", 
                                    `type`="integer"),
                                list(
                                    `name`="test", 
                                    `title`="Wald test", 
                                    `type`="number"),
                                list(
                                    `name`="p", 
                                    `title`="p", 
                                    `type`="number", 
                                    `format`="zto,pvalue")),
                            rows=1))
                        self$add(jmvcore::Table$new(
                            options=options,
                            name="anova",
                            title="Omnibus tests",
                            clearWith=list(
                                "dep",
                                "endo",
                                "exo",
                                "iv"),
                            columns=list(
                                list(
                                    `name`="source", 
                                    `title`="", 
                                    `type`="text"),
                                list(
                                    `name`="df", 
                                    `title`="df", 
                                    `type`="integer"),
                                list(
                                    `name`="f", 
                                    `title`="F", 
                                    `type`="number"),
                                list(
                                    `name`="p", 
                                    `title`="p", 
                                    `type`="number", 
                                    `format`="zto,pvalue"))))
                        self$add(jmvcore::Table$new(
                            options=options,
                            name="coefficients",
                            title="Parameter Estimates (Coefficients)",
                            clearWith=list(
                                "dep",
                                "endo",
                                "exo",
                                "iv",
                                "ci_method",
                                "boot_r",
                                "se_method"),
                            columns=list(
                                list(
                                    `name`="source", 
                                    `title`="Names", 
                                    `type`="text"),
                                list(
                                    `name`="estimate", 
                                    `title`="Estimate", 
                                    `type`="number"),
                                list(
                                    `name`="se", 
                                    `title`="SE", 
                                    `type`="number"),
                                list(
                                    `name`="est.ci.lower", 
                                    `type`="number", 
                                    `title`="Lower", 
                                    `visible`="(estimates_ci)"),
                                list(
                                    `name`="est.ci.upper", 
                                    `type`="number", 
                                    `title`="Upper", 
                                    `visible`="(estimates_ci)"),
                                list(
                                    `name`="beta", 
                                    `type`="number", 
                                    `title`="\u03B2", 
                                    `visible`="(es:beta)", 
                                    `format`="zto"),
                                list(
                                    `name`="beta.ci.lower", 
                                    `type`="number", 
                                    `title`="Lower", 
                                    `visible`="(betas_ci)"),
                                list(
                                    `name`="beta.ci.upper", 
                                    `type`="number", 
                                    `title`="Upper", 
                                    `visible`="(betas_ci)"),
                                list(
                                    `name`="df", 
                                    `title`="df", 
                                    `type`="integer"),
                                list(
                                    `name`="t", 
                                    `title`="t", 
                                    `type`="number"),
                                list(
                                    `name`="p", 
                                    `title`="p", 
                                    `type`="number", 
                                    `format`="zto,pvalue"))))
                        self$add(jmvcore::Table$new(
                            options=options,
                            name="vcov",
                            title="Coefficients Covariances",
                            visible="(vcov)",
                            clearWith=list(
                                "dep",
                                "model_terms",
                                "contrasts",
                                "covs_scale",
                                "dep_scale",
                                "fixed_intercept"),
                            columns=list(
                                list(
                                    `name`="source", 
                                    `title`="Coefficient", 
                                    `type`="text"))))}))$new(options=options))
            self$add(R6::R6Class(
                inherit = jmvcore::Group,
                active = list(
                    standard = function() private$.items[["standard"]]),
                private = list(),
                public=list(
                    initialize=function(options) {
                        super$initialize(
                            options=options,
                            name="diagnostics",
                            title="Diagnostics")
                        self$add(jmvcore::Table$new(
                            options=options,
                            name="standard",
                            title="Model diagnostics",
                            clearWith=list(
                                "dep",
                                "endo",
                                "exo",
                                "iv"),
                            rows=3,
                            columns=list(
                                list(
                                    `name`="source", 
                                    `title`="Test", 
                                    `type`="text"),
                                list(
                                    `name`="df1", 
                                    `title`="df1", 
                                    `type`="integer"),
                                list(
                                    `name`="df2", 
                                    `title`="df2", 
                                    `type`="integer"),
                                list(
                                    `name`="test", 
                                    `title`="Statistic", 
                                    `type`="number"),
                                list(
                                    `name`="p", 
                                    `title`="p", 
                                    `type`="number", 
                                    `format`="zto,pvalue"))))}))$new(options=options))
            self$add(jmvcore::Output$new(
                options=options,
                name="residuals",
                title="Residuals Vales",
                varTitle="`TEST_${ dep }`",
                varDescription="test values",
                clearWith=list(
                    "dep")))}))

endojBase <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "endojBase",
    inherit = jmvcore::Analysis,
    public = list(
        initialize = function(options, data=NULL, datasetId="", analysisId="", revision=0) {
            super$initialize(
                package = "endoj",
                name = "endoj",
                version = c(0,0,1),
                options = options,
                results = endojResults$new(options=options),
                data = data,
                datasetId = datasetId,
                analysisId = analysisId,
                revision = revision,
                pause = NULL,
                completeWhenFilled = TRUE,
                requiresMissings = FALSE)
        }))

#' Instrumental variables model
#'
#' Something here
#' 
#'
#' @examples
#' \dontrun{
#' some code here
#'}
#' @param data the data as a data frame
#' @param .caller .
#' @param .interface .
#' @param dep a string naming the dependent variable from \code{data}; the
#'   variable must be numeric. Not needed if \code{formula} is used.
#' @param exo a vector of strings naming the covariates from \code{data}. Not
#'   needed if \code{formula} is used.
#' @param endo a vector of strings naming the covariates from \code{data}. Not
#'   needed if \code{formula} is used.
#' @param iv a vector of strings naming the covariates from \code{data}. Not
#'   needed if \code{formula} is used.
#' @param es a list of effect sizes to print out. They can be:  \code{"eta"}
#'   for eta-squared, \code{'partEta'} for partial eta-squared, \code{'omega'}
#'   for partial omega-squared, \code{'epsilon'} for partial epsilon-squared,
#'   and \code{'beta'} for standardized coefficients (betas). Default is
#'   \code{"beta"} and \code{"parEta"}.
#' @param estimates_ci \code{TRUE} (default) or \code{FALSE} , parameters CI
#'   in table
#' @param betas_ci \code{TRUE} (default) or \code{FALSE} , parameters CI in
#'   table
#' @param ci_width a number between 50 and 99.9 (default: 95) specifying the
#'   confidence interval width for the plots.
#' @param ci_method .
#' @param se_method .
#' @param boot_r a number bootstrap repetitions.
#' @param vcov \code{TRUE} or \code{FALSE} (default), shows coefficients
#'   covariances
#' @param formula (optional) the formula to use, see the examples
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$info} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$main$r2} \tab \tab \tab \tab \tab a table of R \cr
#'   \code{results$main$anova} \tab \tab \tab \tab \tab a table of ANOVA results \cr
#'   \code{results$main$coefficients} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$main$vcov} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$diagnostics$standard} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$residuals} \tab \tab \tab \tab \tab an output \cr
#' }
#'
#' Tables can be converted to data frames with \code{asDF} or \code{\link{as.data.frame}}. For example:
#'
#' \code{results$info$asDF}
#'
#' \code{as.data.frame(results$info)}
#'
#' @export
endoj <- function(
    data,
    .caller = "lm",
    .interface = "jamovi",
    dep = NULL,
    exo = NULL,
    endo = NULL,
    iv = NULL,
    es = list(
                "beta"),
    estimates_ci = TRUE,
    betas_ci = FALSE,
    ci_width = 95,
    ci_method = "wald",
    se_method = "wald",
    boot_r = 1000,
    vcov = FALSE,
    formula) {

    if ( ! requireNamespace("jmvcore", quietly=TRUE))
        stop("endoj requires jmvcore to be installed (restart may be required)")

    if ( ! missing(formula)) {
        if (missing(dep))
            dep <- jmvcore::marshalFormula(
                formula=formula,
                data=`if`( ! missing(data), data, NULL),
                from="lhs",
                subset="1",
                required=TRUE)
        if (missing(covs))
            covs <- jmvcore::marshalFormula(
                formula=formula,
                data=`if`( ! missing(data), data, NULL),
                from="rhs",
                type="vars",
                permitted="numeric")
        if (missing(model_terms))
            model_terms <- jmvcore::marshalFormula(
                formula=formula,
                data=`if`( ! missing(data), data, NULL),
                from="rhs",
                type="terms")
    }

    if ( ! missing(dep)) dep <- jmvcore::resolveQuo(jmvcore::enquo(dep))
    if ( ! missing(exo)) exo <- jmvcore::resolveQuo(jmvcore::enquo(exo))
    if ( ! missing(endo)) endo <- jmvcore::resolveQuo(jmvcore::enquo(endo))
    if ( ! missing(iv)) iv <- jmvcore::resolveQuo(jmvcore::enquo(iv))
    if (missing(data))
        data <- jmvcore::marshalData(
            parent.frame(),
            `if`( ! missing(dep), dep, NULL),
            `if`( ! missing(exo), exo, NULL),
            `if`( ! missing(endo), endo, NULL),
            `if`( ! missing(iv), iv, NULL))


    options <- endojOptions$new(
        .caller = .caller,
        .interface = .interface,
        dep = dep,
        exo = exo,
        endo = endo,
        iv = iv,
        es = es,
        estimates_ci = estimates_ci,
        betas_ci = betas_ci,
        ci_width = ci_width,
        ci_method = ci_method,
        se_method = se_method,
        boot_r = boot_r,
        vcov = vcov)

    analysis <- endojClass$new(
        options = options,
        data = data)

    analysis$run()

    analysis$results
}
mcfanda/ENDOj documentation built on April 9, 2022, 12:38 a.m.