Psych::Mediate 中的错误:找不到对象

Error in Psych::Mediate: Object Not Found

我正在 运行 对 r 中的数据集进行中介分析,无法弄清楚如何让 psych::mediate 工作——我之前和之前对另一个数据集做过同样的事情没有改变任何东西,但由于某种原因它不能使用这个新数据。

我试过: 1. 将 'condition' 变成 condition.f 因子 2.明确命名DATA a "data.frame" 3.在函数中指定"z"或"mod"等不同的参数 4. 检查所有可变列名称的大写。

None 以上似乎有效。


library(psych)
DATA = STEX_S1_FINALCLEAN

Mediation_RA = psych::mediate( y = "DV_See", x = "Share_T", m = "Seff", data = DATA)
print(Mediation_RA,short=F)

我希望得到具有中介值的完整输出,但得到了:

Error in psych::mediate(y = "DV_See", x = "Share_T", m = "Seff", data = DATA) : 
  object 'ex' not found

我在任何地方都没有看到和反对 'ex',这不是 DATA 数据框中任何列的名称。

根据@r2evans的建议,您可以使用以下修改函数:

mymediate <- function (y, x, m = NULL, data, mod = NULL, z = NULL, n.obs = NULL, 
    use = "pairwise", n.iter = 5000, alpha = 0.05, std = FALSE, 
    plot = TRUE, zero = TRUE, main = "Mediation") 
{
    cl <- match.call()
    if (class(y) == "formula") {
        ps <- fparse(y)
        y <- ps$y
        x <- ps$x
        m <- ps$m
        mod <- ps$prod
        ex <- ps$ex
        x <- x[!ps$x %in% ps$m]
        z <- ps$z
    print(str(ps))
    } else {
      ex = NULL
    }
    all.ab <- NULL
    if (is.numeric(y)) 
        y <- colnames(data)[y]
    if (is.numeric(x)) 
        x <- colnames(data)[x]
    if (!is.null(m)) 
        if (is.numeric(m)) 
            m <- colnames(data)[m]
    if (!is.null(mod)) {
        if (is.numeric(mod)) {
            nmod <- length(mod)
            mod <- colnames(data)[mod]
        }
    }
    if (is.null(mod)) {
        nmod <- 0
    }
    else {
        nmod <- length(mod)
    }
    var.names <- list(IV = x, DV = y, med = m, mod = mod, z = z, 
        ex = ex)
    if (any(!(unlist(var.names) %in% colnames(data)))) {
        stop("Variable names not specified correctly")
    }
    if (ncol(data) == nrow(data)) {
        raw <- FALSE
        if (nmod > 0) {
            stop("Moderation Analysis requires the raw data")
        }
        else {
            data <- data[c(y, x, m, z), c(y, x, m, z)]
        }
    }
    else {
        data <- data[, c(y, x, m, z, ex)]
    }
    if (nmod == 1) {
        mod <- c(x, mod)
        nmod <- length(mod)
    }
    if (!is.matrix(data)) 
        data <- as.matrix(data)
    if ((dim(data)[1] != dim(data)[2])) {
        n.obs = dim(data)[1]
        if (!is.null(mod)) 
            if (zero) 
                data <- scale(data, scale = FALSE)
        C <- cov(data, use = use)
        raw <- TRUE
        if (std) {
            C <- cov2cor(C)
        }
    }
    else {
        raw <- FALSE
        C <- data
        nvar <- ncol(C)
        if (is.null(n.obs)) {
            n.obs <- 1000
            message("The data matrix was a correlation matrix and the number of subjects was not specified. \n n.obs arbitrarily set to 1000")
        }
        if (!is.null(m)) {
            message("The replication data matrices were simulated based upon the specified number of subjects and the observed correlation matrix.")
            eX <- eigen(C)
            data <- matrix(rnorm(nvar * n.obs), n.obs)
            data <- t(eX$vectors %*% diag(sqrt(pmax(eX$values, 
                0)), nvar) %*% t(data))
            colnames(data) <- c(y, x, m)
        }
    }
    if ((nmod > 0) | (!is.null(ex))) {
        if (!raw) {
            stop("Moderation  analysis requires the raw data")
        }
        else {
            if (zero) {
                data <- scale(data, scale = FALSE)
            }
        }
    }
    if (nmod > 0) {
        prods <- matrix(NA, ncol = length(ps$prod), nrow = nrow(data))
        colnames(prods) <- paste0("V", 1:length(ps$prod))
        for (i in 1:length(ps$prod)) {
            prods[, i] <- apply(data[, ps$prod[[i]]], 1, prod)
            colnames(prods)[i] <- paste0(ps$prod[[i]], collapse = "*")
        }
        data <- cbind(data, prods)
        x <- c(x, colnames(prods))
    }
    if (!is.null(ex)) {
        quads <- matrix(NA, ncol = length(ex), nrow = nrow(data))
        colnames(quads) <- ex
        for (i in 1:length(ex)) {
            quads[, i] <- data[, ex[i]] * data[, ex[i]]
            colnames(quads)[i] <- paste0(ex[i], "^2")
        }
        data <- cbind(data, quads)
        x <- c(x, colnames(quads))
    }
    if (raw) {
        C <- cov(data, use = use)
    }
    if (std) {
        C <- cov2cor(C)
    }
    xy <- c(x, y)
    numx <- length(x)
    numy <- length(y)
    if (!is.null(m)) {
        numm <- length(m)
        nxy <- numx + numy
        m.matrix <- C[c(x, m), c(x, m), drop = FALSE]
    }
    else {
        numm <- 0
        nxy <- numx
    }
    df <- n.obs - nxy - 1
    xy.matrix <- C[c(x, m), y, drop = FALSE]
    total.reg <- matReg(x, y, m = m, z = z, C = C, n.obs = n.obs)
    direct <- total.reg$beta
    if (!is.null(z)) {
        colnames(direct) <- paste0(colnames(direct), "*")
        rownames(direct) <- paste0(rownames(direct), "*")
    }
    if (numm > 0) {
        a.reg <- matReg(x = x, y = m, C = C, z = z, n.obs = n.obs)
        b.reg <- matReg(c(x, m), y, C = C, z = z, n.obs = n.obs)
        cprime.reg <- matReg(c(x, m), y, C = C, n.obs = n.obs, 
            z = z)
        a <- a.reg$beta
        b <- b.reg$beta[-(1:numx), , drop = FALSE]
        c <- total.reg$beta
        cprime <- cprime.reg$beta
        all.ab <- matrix(NA, ncol = numm, nrow = numx)
        for (i in 1:numx) {
            all.ab[i, ] <- a[i, ] * t(b[, 1])
        }
        colnames(all.ab) <- m
        rownames(all.ab) <- x
        ab <- a %*% b
        indirect <- c - ab
        if (is.null(n.obs)) {
            message("Bootstrap is not meaningful unless raw data are provided or the number of subjects is specified.")
            mean.boot <- sd.boot <- ci.quant <- boot <- se <- tvalue <- prob <- NA
        }
        else {
            boot <- psych:::boot.mediate(data, x, y, m, z, n.iter = n.iter, 
                std = std, use = use)
            mean.boot <- colMeans(boot)
            sd.boot <- apply(boot, 2, sd)
            ci.quant <- apply(boot, 2, function(x) quantile(x, 
                c(alpha/2, 1 - alpha/2), na.rm = TRUE))
            mean.boot <- matrix(mean.boot, nrow = numx)
            sd.boot <- matrix(sd.boot, nrow = numx)
            ci.ab <- matrix(ci.quant, nrow = 2 * numx * numy)
            boots <- list(mean = mean.boot, sd = sd.boot, ci = ci.quant, 
                ci.ab = ci.ab)
        }
    }
    else {
        a.reg <- b.reg <- reg <- NA
        a <- b <- c <- ab <- cprime <- boot <- boots <- indirect <- cprime.reg <- NA
    }
    if (!is.null(z)) {
        var.names$IV <- paste0(var.names$IV, "*")
        var.names$DV <- paste0(var.names$DV, "*")
        var.names$med <- paste0(var.names$med, "*")
        colnames(C) <- rownames(C) <- paste0(colnames(C), "*")
    }
    result <- list(var.names = var.names, a = a, b = b, ab = ab, 
        all.ab = all.ab, c = c, direct = direct, indirect = indirect, 
        cprime = cprime, total.reg = total.reg, a.reg = a.reg, 
        b.reg = b.reg, cprime.reg = cprime.reg, boot = boots, 
        boot.values = boot, sdnames = colnames(data), data = data, 
        C = C, Call = cl)
    class(result) <- c("psych", "mediate")
    if (plot) {
        if (is.null(m)) {
            moderate.diagram(result)
        }
        else {
            mediate.diagram(result, main = main)
        }
    }
    return(result)
}

您可以使用以下示例测试 mymediate 函数:

library(psych)
mod.k2 <- mymediate(y="OccupAsp", x=c("Intelligence","Siblings","FatherEd","FatherOcc"), 
     m= c(5:6), data=R.kerch, n.obs=767, n.iter=50)
print(mod.k2)