Re: [R] overleap an iteration within a for-loop when error message produced

From: Nicolas Berkowitsch <nicolas.berkowitsch_at_unibas.ch>
Date: Fri, 04 Mar 2011 12:28:23 +0100

Dear Nick,
Dear Philipp,

Thanks for quick responses - it worked!

Below the implemented solution - in case others are interested:

## This will lead to an error message
library (mnormt)
p = matrix(NA,9,1)
for (subject in 2:10) {

     p[subject]=pmnorm(x = subject*c(-.3,1), varcov = matrix(c((-4)^subject,2,2,2),2,2))

     print(subject)
}

## This will NOT lead to an error message library (mnormt)
p = matrix(NA,9,1)
for (subject in 2:10) {

     p[subject]=try(pmnorm(x = subject*c(-.3,1), varcov = matrix(c((-4)^subject,2,2,2),2,2)), silent=FALSE)

     print(subject)
}

Am 04.03.2011 11:17, schrieb Nick Sabbe:
> Check ?tryCatch.
> In most languages, adding try-catch blocks may seriously affect performance
> - I don't know what the impact is in R.
> But perhaps that is not an issue to you.
>
> HTH,
>
>
> Nick Sabbe
> --
> ping: nick.sabbe@ugent.be
> link: http://biomath.ugent.be
> wink: A1.056, Coupure Links 653, 9000 Gent
> ring: 09/264.59.36
>
> -- Do Not Disapprove
>
>
>
>
> -----Original Message-----
> From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On
> Behalf Of Nicolas Berkowitsch
> Sent: vrijdag 4 maart 2011 11:05
> To: r-help_at_r-project.org
> Subject: [R] overleap an iteration within a for-loop when error message
> produced
>
> Dear R-list member,
>
> I'm using the function pmnorm() (-->library(mnormt)) within a for-loop.
> Certain parameter values leads to an error message:
> "(In sqrt(diag(S)) : NaNs produced, In sqrt(1/diag(V)) : NaNs
> produced, In cov2cor(S) : diag(.) had 0 or NA entries; non-finite result
> is doubtful)"
> obviously because "NaNs" were produced.
> Is it possible to tell R that it should overleap the iteration which
> produce the error message?
>
> Here is an example code (does not lead to an error message):
>
> for (subject in 1:10) {
>
> p[subject] = pmnorm(x = subject*c(-.3,1), varcov = diag(2))
>
> }
>
> Assume that the 5th iteration (subject=5) leads to the error message.
> How can I tell R to continue with the 6th iteration?
>
> Thanks a lot for you help and input,
> Nicolas
>
> ____________
>
>
> lic. phil. Nicolas A. J. Berkowitsch
> Universität Basel
> Fakultät für Psychologie
> Economic Psychology
> Missionsstrasse 62a
> CH-4055 Basel
>
> Tel. +41 61 267 05 75
> E-Mail nicolas.berkowitsch_at_unibas.ch
> Web
> http://psycho.unibas.ch/abteilungen/abteilung-details/home/abteilung/economi
> c-psychology/
>
> ______________________________________________
> 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.
>

-- 
____________


lic. phil. Nicolas A. J. Berkowitsch
Universität Basel
Fakultät für Psychologie
Economic Psychology
Missionsstrasse 62a
CH-4055 Basel

Tel.     +41 61 267 05 75
E-Mail   nicolas.berkowitsch_at_unibas.ch
Web      http://psycho.unibas.ch/abteilungen/abteilung-details/home/abteilung/economic-psychology/

______________________________________________
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.
Received on Fri 04 Mar 2011 - 11:31:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 04 Mar 2011 - 12:50:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive