Re: [R] How to say "if error"

2010-06-24 Thread Paul Chatfield

Thanks again Joris - you've been very helpful J

 

From: Joris FA Meys [via R]
[mailto:ml-node+2267176-1824205151-120...@n4.nabble.com] 
Sent: 24 June 2010 16:40
To: Paul Chatfield
Subject: Re: How to say "if error"

 

You could do that using the options, eg : 

set.seed(1) 
x <- rnorm(1:10) 
y <- letters[1:10] 
z <- rnorm(1:10) 

warn <-getOption("warn") 
options(warn=2) 
for (i in list(x,y,z)){ 
  cc <- try(mean(i), silent=T) 
  if(is(cc,"try-error")) {next} 
  print(cc) 
} 
options(warn=warn) 

see ?options under "warn" 

Cheers 
Joris 

On Thu, Jun 24, 2010 at 5:12 PM, Paul Chatfield 
<[hidden email]> wrote: 


> 
> On a similar issue, how can you detect a warning in a loop - e.g. the 
> following gives a warning, so I'd like to set up code to recognise
that and 
> then carry on in a loop 
> 
> x<-rnorm(2);y<-c(1,0) 
> ff<-glm(y/23~x, family=binomial) 
> 
> so this would be incorporated into a loop that might be 
> 
> x<-rnorm(10);y<-rep(c(1,0),5) 
> for (i in 1:10) 
> {ee<-glm(y~x, family=binomial) 
> ff<-glm(y/23~x, family=binomial)} 
> 
> from which I would recognise the warning in ff and not those in ee,
saving 
> results from ee and not from ff. The last bit would be easy adding a
line 
> if(there_is_a_warning_message) {newvector<-NA} else {use results} but
how do 
> you detect the warning message? 
> 
> Thanks all for your feedback so far, 
> 
> Paul 
> -- 
> View this message in context:
http://r.789695.n4.nabble.com/How-to-say-if-error-tp2266619p2267140.html
 
> Sent from the R help mailing list archive at Nabble.com. 
> 
> __ 
> [hidden email] 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. 
> 




-- 
Joris Meys 
Statistical consultant 

Ghent University 
Faculty of Bioscience Engineering 
Department of Applied mathematics, biometrics and process control 

tel : +32 9 264 59 87 
[hidden email] 
--- 
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

__ 
[hidden email] 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. 





View message @
http://r.789695.n4.nabble.com/How-to-say-if-error-tp2266619p2267176.html

To unsubscribe from Re: How to say "if error", click here
< (link removed) 
NoYXRmaWVsZEByZWFkaW5nLmFjLnVrfDIyNjcxNDB8LTE4MjM2NDg5MTM=> . 

 


-- 
View this message in context: 
http://r.789695.n4.nabble.com/How-to-say-if-error-tp2266619p2267182.html
Sent from the R help mailing list archive at Nabble.com.

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] How to say "if error"

2010-06-24 Thread Duncan Murdoch

On 24/06/2010 11:12 AM, Paul Chatfield wrote:

On a similar issue, how can you detect a warning in a loop - e.g. the
following gives a warning, so I'd like to set up code to recognise that and
then carry on in a loop

x<-rnorm(2);y<-c(1,0)
ff<-glm(y/23~x, family=binomial)

so this would be incorporated into a loop that might be

x<-rnorm(10);y<-rep(c(1,0),5)
for (i in 1:10)
{ee<-glm(y~x, family=binomial)
ff<-glm(y/23~x, family=binomial)}

from which I would recognise the warning in ff and not those in ee, saving
results from ee and not from ff. The last bit would be easy adding a line
if(there_is_a_warning_message) {newvector<-NA} else {use results} but how do
you detect the warning message?

Thanks all for your feedback so far,
  


tryCatch(..., warning=function(w) NA)

will work.  For example,

x <- rnorm(10)
y <- rep(c(1,0),5)
ee <- list()
ff <- list()
for (i in 1:10) {
   ee[[i]] <- glm(y~x, family=binomial)
   ff[[i]] <- tryCatch(glm(y/i ~ x, family=binomial), 
warning=function(w) NA)

}

will give 10 fits in ee, but only one in ff (along with 9 NAs).

Duncan Murdoch

__
R-help@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.


Re: [R] How to say "if error"

2010-06-24 Thread Joris Meys
You could do that using the options, eg :

set.seed(1)
x <- rnorm(1:10)
y <- letters[1:10]
z <- rnorm(1:10)

warn <-getOption("warn")
options(warn=2)
for (i in list(x,y,z)){
  cc <- try(mean(i), silent=T)
  if(is(cc,"try-error")) {next}
  print(cc)
}
options(warn=warn)

see ?options under "warn"

Cheers
Joris

On Thu, Jun 24, 2010 at 5:12 PM, Paul Chatfield
 wrote:
>
> On a similar issue, how can you detect a warning in a loop - e.g. the
> following gives a warning, so I'd like to set up code to recognise that and
> then carry on in a loop
>
> x<-rnorm(2);y<-c(1,0)
> ff<-glm(y/23~x, family=binomial)
>
> so this would be incorporated into a loop that might be
>
> x<-rnorm(10);y<-rep(c(1,0),5)
> for (i in 1:10)
> {ee<-glm(y~x, family=binomial)
> ff<-glm(y/23~x, family=binomial)}
>
> from which I would recognise the warning in ff and not those in ee, saving
> results from ee and not from ff. The last bit would be easy adding a line
> if(there_is_a_warning_message) {newvector<-NA} else {use results} but how do
> you detect the warning message?
>
> Thanks all for your feedback so far,
>
> Paul
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/How-to-say-if-error-tp2266619p2267140.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@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.
>



-- 
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
joris.m...@ugent.be
---
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

__
R-help@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.


Re: [R] How to say "if error"

2010-06-24 Thread Paul Chatfield

On a similar issue, how can you detect a warning in a loop - e.g. the
following gives a warning, so I'd like to set up code to recognise that and
then carry on in a loop

x<-rnorm(2);y<-c(1,0)
ff<-glm(y/23~x, family=binomial)

so this would be incorporated into a loop that might be

x<-rnorm(10);y<-rep(c(1,0),5)
for (i in 1:10)
{ee<-glm(y~x, family=binomial)
ff<-glm(y/23~x, family=binomial)}

from which I would recognise the warning in ff and not those in ee, saving
results from ee and not from ff. The last bit would be easy adding a line
if(there_is_a_warning_message) {newvector<-NA} else {use results} but how do
you detect the warning message?

Thanks all for your feedback so far,

Paul
-- 
View this message in context: 
http://r.789695.n4.nabble.com/How-to-say-if-error-tp2266619p2267140.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] How to say "if error"

2010-06-24 Thread Paul Chatfield

That's great.  That solves it.  I can work on eloquence later :)  I just  to
sort out that model problem:

 dof<-numeric(10)
for (i in 1:10){
  x<-rnorm(i-1);y<-rnorm(i-1)
  cc <- try(lm(y~x), silent=T)
  if(is(cc,"try-error")) {dof[i]<-NA}
  else {dof[i]<-(summary(lm(rnorm(10)~rgamma(10,1,1)))$coef[1,1])}
}

Thank you!

Paul
-- 
View this message in context: 
http://r.789695.n4.nabble.com/How-to-say-if-error-tp2266619p2267049.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] How to say "if error"

2010-06-24 Thread Joris Meys
An old-fashioned and I guess also advised-against method would be to
use try() itself, eg :

set.seed(1)
x <- rnorm(1:10)
y <- letters[1:10]
z <- rnorm(1:10)

for (i in list(x,y,z)){
  cc <- try(sum(i), silent=T)
  if(is(cc,"try-error")) {next}
  print(cc)
}

Put silent=F if you want to see the error methods. See also ?try (and ?is )
Cheers
Joris

On Thu, Jun 24, 2010 at 3:34 PM, Duncan Murdoch
 wrote:
> On 24/06/2010 7:06 AM, Paul Chatfield wrote:
>>
>> I've had a look at the conditions in base and I can't get the ones to work
>> I've looked at but it is all new to me.
>> For example, I can work the examples for tryCatch, but it won't print a
>> finally message for me when I apply it to my model.  Even if I could get
>> this to work, I think it would still cause a break e.g.
>> for (j in 1:10)
>> {tryCatch(ifelse(j==5, stop(j), j), finally=print("oh dear"))}
>>
>> Thanks for the suggestion though - any others?
>>
>
> I think you don't want to use finally, which is just code that's guaranteed
> to be executed at the end.  You want to catch the errors and continue.  For
> example,
>
> for (j in 1:10)
> { tryCatch(ifelse(j==5, stop(j), print(j)), error=function(e) {print("caught
> error"); print(e)}) }
>
> Duncan Murdoch
>
> __
> R-help@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.
>



-- 
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
joris.m...@ugent.be
---
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

__
R-help@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.


Re: [R] How to say "if error"

2010-06-24 Thread Duncan Murdoch

On 24/06/2010 7:06 AM, Paul Chatfield wrote:

I've had a look at the conditions in base and I can't get the ones to work
I've looked at but it is all new to me.  


For example, I can work the examples for tryCatch, but it won't print a
finally message for me when I apply it to my model.  Even if I could get
this to work, I think it would still cause a break e.g.
for (j in 1:10)
{tryCatch(ifelse(j==5, stop(j), j), finally=print("oh dear"))}

Thanks for the suggestion though - any others?
  
I think you don't want to use finally, which is just code that's 
guaranteed to be executed at the end.  You want to catch the errors and 
continue.  For example,


for (j in 1:10)
{ tryCatch(ifelse(j==5, stop(j), print(j)), error=function(e) 
{print("caught error"); print(e)}) }


Duncan Murdoch

__
R-help@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.


Re: [R] How to say "if error"

2010-06-24 Thread Paul Chatfield

Thanks Roman - you're right it can do more than I thought.  We're close now
to solving it I feel.  Essentially I'm trying to get the code below to work. 
If the condition is satisfied, it prints 2, but it doesn't save it in z.  I
want it to save it even though there's an error.  Perhaps you can easily see
what I'm missing,

x<-rnorm(0);y<-rnorm(0)
z<-ifelse(tryCatch(lm(y~x), finally=print(2))==1,
NA,lm(rnorm(10)~rnorm(10)))
-- 
View this message in context: 
http://r.789695.n4.nabble.com/How-to-say-if-error-tp2266619p2266782.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] How to say "if error"

2010-06-24 Thread Paul Chatfield

I've had a look at the conditions in base and I can't get the ones to work
I've looked at but it is all new to me.  

For example, I can work the examples for tryCatch, but it won't print a
finally message for me when I apply it to my model.  Even if I could get
this to work, I think it would still cause a break e.g.
for (j in 1:10)
{tryCatch(ifelse(j==5, stop(j), j), finally=print("oh dear"))}

Thanks for the suggestion though - any others?
-- 
View this message in context: 
http://r.789695.n4.nabble.com/How-to-say-if-error-tp2266619p2266760.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] How to say "if error"

2010-06-24 Thread Roman Luštrik

Does ?tryCatch do what you want?
-- 
View this message in context: 
http://r.789695.n4.nabble.com/How-to-say-if-error-tp2266619p2266749.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] How to say "if error"

2010-06-24 Thread Roman Luštrik

I'm sorry, I don't understand your problem to the detail so my suggestion
may be waaay off, but how's this? You can see in vector vec - all ok values
except where there's an error.

n <- 10
vec <- rep(NA, n)
for (j in 1:n)
{tryCatch(ifelse(j==5, vec[j] <- j, j), finally=print("oh dear"))}
vec


Cheers,
Roman



On Thu, Jun 24, 2010 at 1:06 PM, Paul Chatfield [via R] <
ml-node+2266760-1183296220-76...@n4.nabble.com
> wrote:

> I've had a look at the conditions in base and I can't get the ones to work
> I've looked at but it is all new to me.
>
> For example, I can work the examples for tryCatch, but it won't print a
> finally message for me when I apply it to my model.  Even if I could get
> this to work, I think it would still cause a break e.g.
> for (j in 1:10)
> {tryCatch(ifelse(j==5, stop(j), j), finally=print("oh dear"))}
>
> Thanks for the suggestion though - any others?
>
> --
>  View message @
> http://r.789695.n4.nabble.com/How-to-say-if-error-tp2266619p2266760.html
> To unsubscribe from Re: How to say "if error", click here< (link removed) >.
>
>
>


-- 
In God we trust, all others bring data.

-- 
View this message in context: 
http://r.789695.n4.nabble.com/How-to-say-if-error-tp2266619p2266770.html
Sent from the R help mailing list archive at Nabble.com.

[[alternative HTML version deleted]]

__
R-help@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.