ggplot2 eval中的错误(expr,envir,enclos):找不到对象'd'

时间:2015-06-20 01:13:09

标签: r ggplot2

我正在尝试从mardiaTest函数获取ggplot图,但它给出了如下错误:

Error in eval(expr, envir, enclos) : object 'd' not found

以下是可重现的例子:

设置mardiaTest函数的类

   setClass("mardia",
    slots = c(g1p = "numeric", chi.skew="numeric", p.value.skew="numeric", chi.small.skew="numeric",
        p.value.small="numeric", g2p="numeric", z.kurtosis="numeric", p.value.kurt="numeric", dname="character", dataframe="data.frame"))


setGeneric("mardia", function(object) standardGeneric("mardia"))


setMethod("show",
signature = "mardia",
definition = function(object) {
    n=dim(object@dataframe)[1]
    cat("   Mardia's Multivariate Normality Test", "\n", sep = " ")
    cat("---------------------------------------", "\n", sep = " ")
    cat("   data :", object@dname, "\n\n", sep = " ")
    cat("   g1p            :", object@g1p, "\n", sep = " ")
    cat("   chi.skew       :", object@chi.skew, "\n", sep = " ")
    cat("   p.value.skew   :", object@p.value.skew, "\n\n", sep = " ")
    cat("   g2p            :", object@g2p, "\n", sep = " ")
    cat("   z.kurtosis     :", object@z.kurtosis, "\n", sep = " ")
    cat("   p.value.kurt   :", object@p.value.kurt, "\n\n", sep = " ")
    cat("   chi.small.skew :", object@chi.small.skew, "\n", sep = " ")
    cat("   p.value.small  :", object@p.value.small, "\n\n", sep = " ")
    if(n>=20){
        cat(if((object@p.value.skew > 0.05) & (object@p.value.kurt > 0.05)){"   Result         : Data is multivariate normal."}
        else {"   Result          : Data is not multivariate normal."},"\n\n")
    }
    if(n<20){
        cat(if((object@p.value.small > 0.05) & (object@p.value.kurt > 0.05)){"   Result         : Data is multivariate normal."}
        else {"   Result          : Data is not multivariate normal."},"\n\n")
    }
    cat("NOTE: For multivariate normality, both p-values of skewness and kurtosis statistics should be greater than 0.05. If sample size (n) is less than 20 then 'p.value.small' should be used as significance value of skewness instead of 'p.value.skew'.", "\n", sep = " ")
    cat("---------------------------------------", "\n\n", sep = " ")

    invisible(NULL)
})

setClass("hz",
slots = c(HZ = "numeric", p.value="numeric", dname="character", dataframe="data.frame"))


setGeneric("hz", function(object) standardGeneric("hz"))


setMethod("show",
signature = "hz",
definition = function(object) {
    cat("  Henze-Zirkler's Multivariate Normality Test", "\n", sep = " ")
    cat("---------------------------------------------", "\n", sep = " ")
    cat("  data :", object@dname, "\n\n", sep = " ")
    cat("  HZ      :", object@HZ, "\n", sep = " ")
    cat("  p-value :", object@p.value, "\n\n", sep = " ")
    cat(if(object@p.value > 0.05){"  Result  : Data is multivariate normal."}
        else {"  Result  : Data is not multivariate normal."},"\n")
    cat("---------------------------------------------", "\n\n", sep = " ")
    invisible(NULL)
})



setClass("royston",
slots = c(H = "numeric", p.value="numeric", dname="character", dataframe="data.frame"))


setGeneric("royston", function(object) standardGeneric("royston"))


setMethod("show",
signature = "royston",
definition = function(object) {
    cat("  Royston's Multivariate Normality Test", "\n", sep = " ")
    cat("---------------------------------------------", "\n", sep = " ")
    cat("  data :", object@dname, "\n\n", sep = " ")
    cat("  H       :", object@H, "\n", sep = " ")
    cat("  p-value :", object@p.value, "\n\n", sep = " ")
    cat(if(object@p.value > 0.05){"  Result  : Data is multivariate normal."}
        else {"  Result  : Data is not multivariate normal."},"\n")
    cat("---------------------------------------------", "\n\n", sep = " ")
    invisible(NULL)
})


setClass("dh",
slots = c(TS = "numeric", p.value="numeric", dname="character", dataframe="data.frame"))


setGeneric("dh", function(object) standardGeneric("dh"))


setMethod("show",
signature = "dh",
definition = function(object) {
    cat("  Doornick-Hansen's Multivariate Normality Test", "\n", sep = " ")
    cat("---------------------------------------------", "\n", sep = " ")
    cat("  data :", object@dname, "\n\n", sep = " ")
    cat("  DH      :", object@TS, "\n", sep = " ")
    cat("  p-value :", object@p.value, "\n\n", sep = " ")
    cat(if(object@p.value > 0.05){"  Result  : Data is multivariate normal."}
    else {"  Result  : Data is not multivariate normal."},"\n")
    cat("---------------------------------------------", "\n\n", sep = " ")
    invisible(NULL)
})

mardiaTest功能

mardiaTest <- function (data, cov = TRUE, qqplot = FALSE) 
            {
                dataframe=as.data.frame(data)
                dname <- deparse(substitute(data))
                data <- as.matrix(data)
                n <- dim(data)[1]
                p <- dim(data)[2]
                data.org <- data
                data <- scale(data, scale = FALSE)
                if (cov) {
                    S <- ((n - 1)/n) * cov(data)
                }
                else {
                    S <- cov(data)
                }
                D <- data %*% solve(S) %*% t(data)
                g1p <- sum(D^3)/n^2
                g2p <- sum(diag((D^2)))/n
                df <- p * (p + 1) * (p + 2)/6
                k <- (p + 1) * (n + 1) * (n + 3)/(n * ((n + 1) * (p + 1) - 
                                                           6))
                small.skew <- n * k * g1p/6
                skew <- n * g1p/6
                kurt <- (g2p - p * (p + 2)) * sqrt(n/(8 * p * (p + 2)))
                p.skew <- pchisq(skew, df, lower.tail = FALSE)
                p.small <- pchisq(small.skew, df, lower.tail = FALSE)
                p.kurt <- 2 * (1 - pnorm(abs(kurt)))

            if (qqplot) {
                 d <- diag(D)
                 r <- rank(d)
                chi2q <- qchisq((r - 0.5)/n, p)


                #plot(d, chi2q, pch = 19, main = "Chi-Square Q-Q Plot",
                   #  xlab = "Squared Mahalanobis Distance",ylab="Chi-Square Quantile")
                #abline(0, 1,lwd = 2, col = "black")

                qqPlotPrint = ggplot()+geom_point(aes(d, chi2q),shape=16, size=3)+
                geom_abline(intercept =0, slope =1,color="red",size=1)+
                xlab("Squared Mahalanobis Distance")+
                ylab("Chi-Square Quantile")+
                ggtitle("Chi-Square Q-Q Plot")+
                theme(plot.title = element_text(lineheight=.8, face="bold"))

                print(qqPlotPrint)
            }
            result <- new("mardia", g1p = g1p, chi.skew = skew, p.value.skew = p.skew,
                          chi.small.skew = small.skew, p.value.small = p.small, g2p = g2p,
                          z.kurtosis = kurt, p.value.kurt = p.kurt, dname = dname, dataframe = dataframe)

            result
        }

实施例

    library(ggplot2)
    data = iris[1:50,1:2]

    mardiaTest(data, qqplot=T)

获取以下错误:

Error in eval(expr, envir, enclos) : object 'd' not found

虽然它在函数外部工作,但它不能在函数内部工作。

0 个答案:

没有答案