Nicolas Berkowitsch
2011-Mar-04 10:04 UTC
[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/economic-psychology/
Philipp Pagel
2011-Mar-04 10:15 UTC
[R] overleap an iteration within a for-loop when error message produced
> Assume that the 5th iteration (subject=5) leads to the error > message. How can I tell R to continue with the 6th iteration?try or tryCatch are probably what you want. cu Philipp -- Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan Maximus-von-Imhof-Forum 3 85354 Freising, Germany http://webclu.bio.wzw.tum.de/~pagel/
Nicolas Berkowitsch
2011-Mar-04 11:28 UTC
[R] overleap an iteration within a for-loop when error message produced
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 at 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/