Skip to content
Prev 374735 / 398513 Next

Using tryCatch in a for loop

Ah ok?this solves it for me, thank you!?

Bailey




From: Bert Gunter <bgunter.4567 at gmail.com>
Sent: May 22, 2018 4:42 PM
To: Bailey Hewitt
Cc: Daniel Nordlund; r-help at R-project.org
Subject: Re: [R] Using tryCatch in a for loop
?  








No. If your ouput is a numeric "matrix", it cannot include alpha. Columns in a data frame can be of different classes, but each column must be single class.

 and finally, of course, see ?cat -- I think you are misusing it. If you simply want to return "somestuff", your function should be:

 function(w) {"somestuff"}

 not 

 function(w) {cat("somestuff")}

 As usual, apologies if I have misunderstood. Caveat emptor.

 Cheers,
 Bert






Bert Gunter

"The trouble with having an open mind is that people keep coming along and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Tue, May 22, 2018 at 1:11 PM, Bailey Hewitt <bailster at hotmail.com> wrote:
Data and code as promised:

#Creating a test dataset
Year<- c(2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014)
Lake1<- c(2, 4, 5, 2, 1, 1, 2, 3, 4, 5, 6, 2, 3, 1, 2)
Lake2<- c(1, 3, -1, 4, -2, 1, 2, 3, 4, 5, 6, 2, 3, 1, 2)
Lake3<- c(1, 2, 5, -3, 1, 1, 2, 3, 4, 5, 6, 2, 3, 1, 2)
Lake4<- c(1, 1, 1, 1, 1, 1, 1, 250, 240, 240, 240, 240, 240, 239, 255)
mydata<- data.frame(Year, Lake1, Lake2, Lake3, Lake4)

#Running a for loop that indicates when an error or warning occurs
y<- mydata[,2:5]
year <- mydata$Year
regimeshift <- data.frame()
for (i in 1:4){
? tryCatch({
? ? y.val <- y[,i]
? ? lin.reg <- lm(y.val~year, mydata)
? ? seg.reg <- segmented.lm(lin.reg, seg.Z = ~ year, psi = NA, control = seg.control(stop.if.error = FALSE, n.boot = 0, it.max = 20))
? ? RSyear <- summary(seg.reg)$psi [1,2]
? ? SlopeRegime1 <- summary(seg.reg)$coefficients[2,1] 
? ? SlopeDiff <- summary(seg.reg)$coefficients[3,1] 
? ? new.regimeshift <- data.frame(RSyear=RSyear, SlopeRegime1=SlopeRegime1, SlopeDiff=SlopeDiff)
? ? rownames(new.regimeshift) <- colnames(y)[i]
? ? regimeshift <- rbind(regimeshift,new.regimeshift)
? ? print(regimeshift)
? }, error= function(e) {cat("Error", "\n")},
? ? warning= function(w) {cat("Warning", "\n")})
}

Bailey




From: Daniel Nordlund <djnordlund at gmail.com>
Sent: May 22, 2018 3:06 PM
To: Bailey Hewitt; Bert Gunter


Cc: r-help at R-project.org
Subject: Re: [R] Using tryCatch in a for loop
?
On 5/22/2018 11:32 AM, Bailey Hewitt wrote:
My suggestion is that you get your code to work on a single iteration 
before running in a loop and wrapping the code in tryCatch

I ran your "set-up" code (before the loop).? then extracted your code 
from the loop, set i <- 1, and then ran the code.? Here is what I got:

?> i <- 1
?>???? y.val <- y[,i]
?>???? lin.reg <- lm(y.val~year, mydata)
?>???? seg.reg <- segmented.lm(lin.reg, seg.Z = ~ year, psi = NA, 
control = seg.control(stop.if.error = FALSE, n.boot = 0, it.max = 20))
Warning message:
No breakpoint estimated
?>???? RSyear <- summary(seg.reg)$psi [1,2]
?>???? SlopeRegime1 <- summary(seg.reg)$coefficients[2,1]
?>???? SlopeDiff <- summary(seg.reg)$coefficients[3,1]
Error in summary(seg.reg)$coefficients[3, 1] : subscript out of bounds
?>???? new.regimeshift <- data.frame(RSyear=RSyear, 
SlopeRegime1=SlopeRegime1, SlopeDiff=SlopeDiff)
Error in data.frame(RSyear = RSyear, SlopeRegime1 = SlopeRegime1, 
SlopeDiff = SlopeDiff) :
?? object 'SlopeDiff' not found
?>???? rownames(new.regimeshift) <- colnames(y)[i]
Error in rownames(new.regimeshift) <- colnames(y)[i] :
?? object 'new.regimeshift' not found
?>???? regimeshift <- rbind(regimeshift,new.regimeshift)
Error in rbind(regimeshift, new.regimeshift) :
?? object 'new.regimeshift' not found
?>???? print(regimeshift)

the immediate problems that show up are
1.? there is no variable psi the summary(seg.reg) object (RSyear is NULL).
2.? the coefficients[] matrix in the summary(seg.reg) object does not 
have 3 rows, so you get an error and SlopeDiff is not created.

You need to correct these problems, and any others, so that your code 
runs correctly when there are no data problems.? Then you can worry 
about trapping errors in the case where there are data problems.

Hope this is helpful,

Dan
Message-ID: <YQXPR0101MB08058A525C87505F5E2657D0D9940@YQXPR0101MB0805.CANPRD01.PROD.OUTLOOK.COM>
In-Reply-To: <CAGxFJbSUL7tnzL5Zb_0_V0jFiUNZi2=FrfEyaJgEASRgLQ7aOA@mail.gmail.com>