Hello All, I have been trying to use a for loop to run segmented regressions (from?R package segmented)?on many columns of data in a data frame with the end goal of writing a new file with the following columns: column title, breakpoint year, slope, and difference in slope. Unfortunately, when one of the columns doesn't have a breakpoint the code stops and provides?an error or warning. I would like the loop to keep running regardless of the error/warning but I want it to write that it did encounter an error or warning. Based on my needs I found that tryCatch appears to do what I need. I have looked at multiple examples, blogs, etc. as well as gone over the documentation and Hadley Wickham's document in Advanced R but I am still a novice when it comes to coding and I am having a hard time getting the code to work properly. Below is the code I have developed thus far (with a test dataset as an example): #Creating a test dataset Year<- c(2000, 2001, 2002, 2003, 2004) Lake1<- c(2, 4, 5, 2, 1) Lake2<- c(1, 3, -1, 4, -2) Lake3<- c(1, 2, 5, -3, 1) mydata<- data.frame(Year, Lake1, Lake2, Lake3) #Running a for loop that indicates when an error or warning occurs y<- mydata[,2:4] year <- mydata$Year regimeshift <- data.frame() for (i in 1:3){ ? 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")}) } Any ideas or suggestions you might have are greatly?appreciated! Bailey Hewitt PhD Candidate York University Ontario, Canada
Others may have greater insight, but my response is: Exactly what did or didn't happen that makes you say the code didn't work? That is, what did or didn't you get when you ran it compared to your expectations? 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 10:55 AM, Bailey Hewitt <bailster at hotmail.com> wrote:> Hello All, > > I have been trying to use a for loop to run segmented regressions (from R > package segmented) on many columns of data in a data frame with the end > goal of writing a new file with the following columns: column title, > breakpoint year, slope, and difference in slope. Unfortunately, when one of > the columns doesn't have a breakpoint the code stops and provides an error > or warning. I would like the loop to keep running regardless of the > error/warning but I want it to write that it did encounter an error or > warning. Based on my needs I found that tryCatch appears to do what I need. > I have looked at multiple examples, blogs, etc. as well as gone over the > documentation and Hadley Wickham's document in Advanced R but I am still a > novice when it comes to coding and I am having a hard time getting the code > to work properly. Below is the code I have developed thus far (with a test > dataset as an example): > > #Creating a test dataset > Year<- c(2000, 2001, 2002, 2003, 2004) > Lake1<- c(2, 4, 5, 2, 1) > Lake2<- c(1, 3, -1, 4, -2) > Lake3<- c(1, 2, 5, -3, 1) > mydata<- data.frame(Year, Lake1, Lake2, Lake3) > > #Running a for loop that indicates when an error or warning occurs > y<- mydata[,2:4] > year <- mydata$Year > regimeshift <- data.frame() > for (i in 1:3){ > 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")}) > } > > Any ideas or suggestions you might have are greatly appreciated! > > Bailey Hewitt > PhD Candidate > York University > Ontario, Canada > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Hi Bert, Thank you for the quick response!? In its current?state?the code?prints three lines that say "warning". What I was expecting is that I would?get?a matrix with 4 columns, 1. column names (from the original data, ex. Lake1) 2. breakpoint year 3. slope 4. slope difference from the first to the?second segment of the segmented regression. Each row in the matrix would be the results of the segmented regression test for each lake in the original data frame, so?Lake1 results would be in row 2 (row 1 would be titles) and so on. If any of?this is confusing please let me know and I will clarify! Thanks! Bailey? From: Bert Gunter <bgunter.4567 at gmail.com> Sent: May 22, 2018 2:13 PM To: Bailey Hewitt Cc: r-help at R-project.org Subject: Re: [R] Using tryCatch in a for loop ? Others may have greater insight, but my response is: Exactly what did or didn't happen that makes you say the code didn't work? That is, what did or didn't you get when you ran it compared to your expectations? 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 10:55 AM, Bailey Hewitt <bailster at hotmail.com> wrote: Hello All, I have been trying to use a for loop to run segmented regressions (from?R package segmented)?on many columns? of data in a data frame with the end goal of writing a new file with the following columns: column title, breakpoint year, slope, and difference in slope. Unfortunately, when one of the columns doesn't have a breakpoint the code stops and provides?an error or warning. I would like the loop to keep running regardless of the error/warning but I want it to write that it did encounter an error or warning. Based on my needs I found that tryCatch appears to do what I need. I have looked at multiple examples, blogs, etc. as well as gone over the documentation and Hadley Wickham's document in Advanced R but I am still a novice when it comes to coding and I am having a hard time getting the code to work properly. Below is the code I have developed thus far (with a test dataset as an example): #Creating a test dataset Year<- c(2000, 2001, 2002, 2003, 2004) Lake1<- c(2, 4, 5, 2, 1) Lake2<- c(1, 3, -1, 4, -2) Lake3<- c(1, 2, 5, -3, 1) mydata<- data.frame(Year, Lake1, Lake2, Lake3) #Running a for loop that indicates when an error or warning occurs y<- mydata[,2:4] year <- mydata$Year regimeshift <- data.frame() for (i in 1:3){ ? 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")}) } Any ideas or suggestions you might have are greatly?appreciated! Bailey Hewitt PhD Candidate York University Ontario, Canada ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
(practically) ALWAYS respond to the list. I have cc'ed them here. Others may (almost always do!) have greater insight/better answers than I. In this case, I have no clue, but presumably your call is wrong, but as I have no experience with the segmented.lm function, I don't care to track down how exactly. Your tryCatch call seems to be fine, however. -- 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 11:30 AM, Bailey Hewitt <bailster at hotmail.com> wrote:> Hi Bert, > > > Thank you for the quick response! > > > In its current state the code prints three lines that say "warning". What > I was expecting is that I would get a matrix with 4 columns, 1. column > names (from the original data, ex. Lake1) 2. breakpoint year 3. slope 4. > slope difference from the first to the second segment of the segmented > regression. Each row in the matrix would be the results of the segmented > regression test for each lake in the original data frame, so Lake1 results > would be in row 2 (row 1 would be titles) and so on. If any of this is > confusing please let me know and I will clarify! > > > Thanks! > > > Bailey > > > ------------------------------ > *From:* Bert Gunter <bgunter.4567 at gmail.com> > *Sent:* May 22, 2018 2:13 PM > *To:* Bailey Hewitt > *Cc:* r-help at R-project.org > *Subject:* Re: [R] Using tryCatch in a for loop > > Others may have greater insight, but my response is: Exactly what did or > didn't happen that makes you say the code didn't work? That is, what did or > didn't you get when you ran it compared to your expectations? > > 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 10:55 AM, Bailey Hewitt <bailster at hotmail.com> > wrote: > > Hello All, > > I have been trying to use a for loop to run segmented regressions (from R > package segmented) on many columns of data in a data frame with the end > goal of writing a new file with the following columns: column title, > breakpoint year, slope, and difference in slope. Unfortunately, when one of > the columns doesn't have a breakpoint the code stops and provides an error > or warning. I would like the loop to keep running regardless of the > error/warning but I want it to write that it did encounter an error or > warning. Based on my needs I found that tryCatch appears to do what I need. > I have looked at multiple examples, blogs, etc. as well as gone over the > documentation and Hadley Wickham's document in Advanced R but I am still a > novice when it comes to coding and I am having a hard time getting the code > to work properly. Below is the code I have developed thus far (with a test > dataset as an example): > > #Creating a test dataset > Year<- c(2000, 2001, 2002, 2003, 2004) > Lake1<- c(2, 4, 5, 2, 1) > Lake2<- c(1, 3, -1, 4, -2) > Lake3<- c(1, 2, 5, -3, 1) > mydata<- data.frame(Year, Lake1, Lake2, Lake3) > > #Running a for loop that indicates when an error or warning occurs > y<- mydata[,2:4] > year <- mydata$Year > regimeshift <- data.frame() > for (i in 1:3){ > 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")}) > } > > Any ideas or suggestions you might have are greatly appreciated! > > Bailey Hewitt > PhD Candidate > York University > Ontario, Canada > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posti > ng-guide.html > and provide commented, minimal, self-contained, reproducible code. > > >[[alternative HTML version deleted]]