Moumita Das
2009-Jun-25 14:59 UTC
[R] Error: system is computationally singular: reciprocal condition number
I get this error while computing partial correlation. *Error in solve.default(Szz) : system is computationally singular: reciprocal condition number 4.90109e-18* Why is it?Can anyone give me some idea ,how do i get rid it it? This is the function i use for calculating partial correlation. pcor.mat <- function(x,y,z,method="p",na.rm=T){ x <- c(x) y <- c(y) z <- as.data.frame(z) if(dim(z)[2] == 0){ stop("There should be given data\n") } data <- data.frame(x,y,z) if(na.rm == T){ data = na.omit(data) } xdata <- na.omit(data.frame(data[,c(1,2)])) Sxx <- cov(xdata,xdata,m=method) xzdata <- na.omit(data) xdata <- data.frame(xzdata[,c(1,2)]) zdata <- data.frame(xzdata[,-c(1,2)]) Sxz <- cov(xdata,zdata,m=method) zdata <- na.omit(data.frame(data[,-c(1,2)])) Szz <- cov(zdata,zdata,m=method) # is Szz positive definite? zz.ev <- eigen(Szz)$values if(min(zz.ev)[1]<0){ stop("\'Szz\' is not positive definite!\n") } # partial correlation Sxx.z <- Sxx - Sxz %*% solve(Szz) %*% t(Sxz) print(Sxx.z) # this gets printed rxx.z <- cov2cor(Sxx.z)[1,2] #some problem in this function function (V) { print("cov2cor") p <- (d <- dim(V))[1] if (!is.numeric(V) || length(d) != 2L || p != d[2L]) stop("'V' is not a square numeric matrix") Is <- sqrt(1/diag(V)) if (any(!is.finite(Is))) warning("diag(.) had 0 or NA entries; non-finite result is doubtful") r <- V r[] <- Is * V * rep(Is, each = p) r[cbind(1L:p, 1L:p)] <- 1 r } return(rxx.z) } -- Thanks Moumita [[alternative HTML version deleted]]
Ravi Varadhan
2009-Jun-25 16:11 UTC
[R] Error: system is computationally singular: reciprocal conditionnumber
Your covariance matrix Szz is not positive definite. It is singular. The following test that you are doing is neither necessary nor useful: zz.ev <- eigen(Szz)$values if(min(zz.ev)[1]<0){ stop("\'Szz\' is not positive definite!\n") } You may want to use Moore-Penrose inverse, also known as generalized inverse or pseudoinverse to overcome this problem. This approach uses singular-value decomposition (SVD). Take a look at the "corpcor" package. Ravi. ---------------------------------------------------------------------------- ------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: rvaradhan at jhmi.edu Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty_personal_pages/Varadhan.h tml ---------------------------------------------------------------------------- -------- -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Moumita Das Sent: Thursday, June 25, 2009 10:59 AM To: r-help at r-project.org Subject: [R] Error: system is computationally singular: reciprocal conditionnumber I get this error while computing partial correlation. *Error in solve.default(Szz) : system is computationally singular: reciprocal condition number 4.90109e-18* Why is it?Can anyone give me some idea ,how do i get rid it it? This is the function i use for calculating partial correlation. pcor.mat <- function(x,y,z,method="p",na.rm=T){ x <- c(x) y <- c(y) z <- as.data.frame(z) if(dim(z)[2] == 0){ stop("There should be given data\n") } data <- data.frame(x,y,z) if(na.rm == T){ data = na.omit(data) } xdata <- na.omit(data.frame(data[,c(1,2)])) Sxx <- cov(xdata,xdata,m=method) xzdata <- na.omit(data) xdata <- data.frame(xzdata[,c(1,2)]) zdata <- data.frame(xzdata[,-c(1,2)]) Sxz <- cov(xdata,zdata,m=method) zdata <- na.omit(data.frame(data[,-c(1,2)])) Szz <- cov(zdata,zdata,m=method) # is Szz positive definite? zz.ev <- eigen(Szz)$values if(min(zz.ev)[1]<0){ stop("\'Szz\' is not positive definite!\n") } # partial correlation Sxx.z <- Sxx - Sxz %*% solve(Szz) %*% t(Sxz) print(Sxx.z) # this gets printed rxx.z <- cov2cor(Sxx.z)[1,2] #some problem in this function function (V) { print("cov2cor") p <- (d <- dim(V))[1] if (!is.numeric(V) || length(d) != 2L || p != d[2L]) stop("'V' is not a square numeric matrix") Is <- sqrt(1/diag(V)) if (any(!is.finite(Is))) warning("diag(.) had 0 or NA entries; non-finite result is doubtful") r <- V r[] <- Is * V * rep(Is, each = p) r[cbind(1L:p, 1L:p)] <- 1 r } return(rxx.z) } -- Thanks Moumita [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Moumita Das
2009-Jun-28 13:58 UTC
[R] Error: system is computationally singular: reciprocal condition number
I get this error .... on using the, traceback() function :--> traceback()10: .Call("La_dgesv", a, b, tol, PACKAGE = "base") 9: solve.default(Szz) 8: solve(Szz) 7: pcor.mat(firstvalue, secondvalue, third_var, method, na.rm = T) 6: PartialCorr_Calculation(value1, value2, third_var, method = "pearson", na.rm = T) 5: Partial(contrld_third_var(rowvalues$matrix1, rowvalues$matrix2, x <- stringOfItemCategoryToDataFrameOfItemCategory, item_category_table, pcor_type <- "ic"), data1, data2, pcor_thirdvar_type <- "all") 4: main() 3: eval.with.vis(expr, envir, enclos) 2: eval.with.vis(ei, envir) 1: source("correlationFP.R") what is this error ".*Call("La_dgesv", a, b, tol, PACKAGE = "base")*" how can i rectify it? I don't know statistics...going mad ,debugging this problem.. any help is highly appreciated !! :) Thanks in advance Moumita On Thu, Jun 25, 2009 at 8:29 PM, Moumita Das <das.moumita.online@gmail.com>wrote:> > I get this error while computing partial correlation. > > > *Error in solve.default(Szz) : > system is computationally singular: reciprocal condition number > 4.90109e-18* > > Why is it?Can anyone give me some idea ,how do i get rid it it? > > This is the function i use for calculating partial correlation. > > > pcor.mat <- function(x,y,z,method="p",na.rm=T){ > > > x <- c(x) > y <- c(y) > z <- as.data.frame(z) > > > > if(dim(z)[2] == 0){ > stop("There should be given data\n") > } > > data <- data.frame(x,y,z) > > if(na.rm == T){ > data = na.omit(data) > } > > xdata <- na.omit(data.frame(data[,c(1,2)])) > Sxx <- cov(xdata,xdata,m=method) > > xzdata <- na.omit(data) > xdata <- data.frame(xzdata[,c(1,2)]) > zdata <- data.frame(xzdata[,-c(1,2)]) > Sxz <- cov(xdata,zdata,m=method) > > zdata <- na.omit(data.frame(data[,-c(1,2)])) > Szz <- cov(zdata,zdata,m=method) > > > # is Szz positive definite? > zz.ev <- eigen(Szz)$values > if(min(zz.ev)[1]<0){ > > stop("\'Szz\' is not positive definite!\n") > } > > # partial correlation > Sxx.z <- Sxx - Sxz %*% solve(Szz) %*% t(Sxz) > > print(Sxx.z) # this gets printed > > rxx.z <- cov2cor(Sxx.z)[1,2] #some problem in this function > function (V) > { > print("cov2cor") > p <- (d <- dim(V))[1] > if (!is.numeric(V) || length(d) != 2L || p != d[2L]) > stop("'V' is not a square numeric matrix") > Is <- sqrt(1/diag(V)) > if (any(!is.finite(Is))) > warning("diag(.) had 0 or NA entries; non-finite result is > doubtful") > r <- V > r[] <- Is * V * rep(Is, each = p) > r[cbind(1L:p, 1L:p)] <- 1 > r > } > return(rxx.z) > } > > -- > Thanks > Moumita >-- Thanks Moumita [[alternative HTML version deleted]]