r - ggplot2 Error in eval(expr, envir, enclos) : object 'd' not found -
i trying ggplot graph mardiatest function gives me error follows:
error in eval(expr, envir, enclos) : object 'd' not found
here reproducible example:
setting class mardiatest function
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 multivariate normal."} else {" result : data not multivariate normal."},"\n\n") } if(n<20){ cat(if((object@p.value.small > 0.05) & (object@p.value.kurt > 0.05)){" result : data multivariate normal."} else {" result : data not multivariate normal."},"\n\n") } cat("note: multivariate normality, both p-values of skewness , kurtosis statistics should greater 0.05. if sample size (n) less 20 'p.value.small' should used 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 multivariate normal."} else {" result : data 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 multivariate normal."} else {" result : data 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 multivariate normal."} else {" result : data not multivariate normal."},"\n") cat("---------------------------------------------", "\n\n", sep = " ") invisible(null) })
mardiatest function
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 }
example
library(ggplot2) data = iris[1:50,1:2] mardiatest(data, qqplot=t)
getting following error:
error in eval(expr, envir, enclos) : object 'd' not found
although working outside of function, not working inside of it.
Comments
Post a Comment