Dear R Users, I have the following glm, which I am running several times in a loop (I am not including the full code): reduced_model <- NULL; full_model <- NULL; reduced_model <- try(glm.fit(X4,n,family=poisson(link="log"))) full_model <- try(glm.fit(X5,n,family=poisson(link="log"))); On some occasions, an error is produced, which is why I have attempted to work around this with try(). I then have the following statement, which checks if full_model or reduced_model are still NULL. if (is.null(reduced_model) || is.null(full_model)) p <- NA else p <- pchisq(reduced_model$deviance - full_model$deviance, reduced_model$df.residual - full_model$df.residual, lower.tailFALSE); This is not solving my problem meaning my program still stops and gives me the following error: Error in ifelse(y == 0, 1, y/mu) : dim<- : dims [product 9] do not match the length of object [18] Error in pmatrix[perm, ] <- apply(pairs.subset, 1, getLRTp2) : nothing to replace with Can you help me figure out what I have done incorrectly? Thanks for your time, Juliet
Neither of reduced_model not full_model will ever be NULL. If this fails, they inherit from class "try-error". See ?try. So you want inherits(reduced_model, "try-error") || inherits(full_model, "try-error") On Thu, 14 Feb 2008, Juliet Hannah wrote:> Dear R Users, > > I have the following glm, which I am running several times in a loop > (I am not including the full code): > > reduced_model <- NULL; > full_model <- NULL; > reduced_model <- try(glm.fit(X4,n,family=poisson(link="log"))) > full_model <- try(glm.fit(X5,n,family=poisson(link="log"))); > > On some occasions, an error is produced, which is why I have attempted > to work around this with try(). > > I then have the following statement, which checks if full_model or > reduced_model are still NULL. > > if (is.null(reduced_model) || is.null(full_model)) p <- NA else p <- > pchisq(reduced_model$deviance - full_model$deviance, > reduced_model$df.residual - full_model$df.residual, lower.tail> FALSE); > > This is not solving my problem meaning my program still stops and > gives me the following error: > > Error in ifelse(y == 0, 1, y/mu) : dim<- : dims [product 9] do not > match the length of object [18] > Error in pmatrix[perm, ] <- apply(pairs.subset, 1, getLRTp2) : > nothing to replace with > > > Can you help me figure out what I have done incorrectly? > > Thanks for your time, > > Juliet > > ______________________________________________ > 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Dear R Users, I incorporated the suggestions given to me, which solved the problem of the program ending. However, once an error occurs, and the value NA is assigned, the program does not run correctly after this point. All values assigned after this point are NA. Here is my function. getLRTp <- function(index) { n <- as.matrix(table(myData[,index[1]],myData[,index[2]],permute.response)) n[n[,1]==0] <- 1 reduced_model <- try(glm.fit(X4,n,family=poisson(link="log"))) full_model <- try(glm.fit(X5,n,family=poisson(link="log"))); if (inherits(reduced_model,"try-error") || inherits(full_model,"try-error")) { p <- NA } else { p <- pchisq(reduced_model$deviance - full_model$deviance, reduced_model$df.residual - full_model$df.residual, lower.tail= FALSE) } } which is called in this manner: for (perm in 1:nperm) { permute.response <- sample(response,replace=TRUE) pmatrix[perm,] <- apply(pairs.subset, 1, getLRTp) } Again, I have included only the relevant part of the code. If the glm is succeeds, p is a p-value. On the occassion that it does not, p is NA. pmatrix (my result) ends up looking like: [,1] [,2] [,3] [,4] [,5] [1,] 0.6291316 0.08900112 0.06693455 6.401101e-06 0.06865330 [2,] 0.6259834 0.21140489 0.06722201 6.401101e-06 0.06833421 [3,] NA NA NA NA NA [4,] NA NA NA NA NA [5,] NA NA NA NA NA It seems that after encountering an error and NA is assigned, it never recovers. Is this is programming error? Thanks for your time, Juliet