Dear users, *******I have a function f to simulate data from a model (example below used only to show my problems) f<-function(n,mean1){ a<-matrix(rnorm(n, mean1 , sd = 1),ncol=5) b<-matrix(runif(n),ncol=5) data<-rbind(a,b) out<-data out} *********I want to simulate 1000 datasets (here only 5) so I use S<-list() for (i in 1:5){ S[[i]]<-f(n=10,mean1=0)} ******I have a very complicated function for estimation of a model which I want to apply to Each one of the above simulated datasets fun<-function(data){data<-as.matrix(data) sink(' Example.txt',append=TRUE) cat("\n***********************\nEstimation \n********************\nDataset Sim : ", i ) d<-data%*%t(data) s<-solve(d) print(s) out<-s out } results<-list() for (i in 1:5){results[[i]]<-fun(data=S[[i]])} My questions are: 1) for some datasets system is computational singular and this causes execution of the for to stop.By this way I have only results until this problem happens.How can I pass over the execution for this step and have results for All other datasets for which function fun is applicable? 2) After some runs to my program, I receive this error message someError in sink("output.txt") : sink stack is full . How can I solve this problem, as I want to have results of my program for 1000 datasets. 3) Using for is the correct way to run my proram for a list Thanks alot Evgenia -- View this message in context: http://r.789695.n4.nabble.com/Problems-when-Apply-a-script-to-a-list-tp2339403p2339403.html Sent from the R help mailing list archive at Nabble.com.
Answers below. On Thu, Aug 26, 2010 at 11:20 AM, Evgenia <evgts at aueb.gr> wrote:> > Dear users, > > *******I have a function f to simulate data from a model (example below used > only to show my problems) > > f<-function(n,mean1){ > a<-matrix(rnorm(n, mean1 , sd = 1),ncol=5) > b<-matrix(runif(n),ncol=5) > data<-rbind(a,b) > out<-data > out} > > *********I want to simulate 1000 datasets (here only 5) so I use > S<-list() > > for (i in 1:5){ > S[[i]]<-f(n=10,mean1=0)} > > ******I have a very complicated function ?for estimation of a model which I > want to apply to Each one of the above simulated datasets > > fun<-function(data){data<-as.matrix(data) > sink(' Example.txt',append=TRUE) > ? ? ? ? ?cat("\n***********************\nEstimation > \n********************\nDataset Sim : ", > ? ? ? ? ? ?i ) > d<-data%*%t(data) > s<-solve(d) > print(s) > out<-s > out > } > results<-list() > for (i in 1:5){results[[i]]<-fun(data=S[[i]])} > > > My questions are: > 1) for some datasets system is computational singular and this causes > execution of the for to stop.By this way I have only results until this > problem happens.How can I pass over the execution for this step and have > results for All other datasets for which function fun is applicable?see ?try, or ?tryCatch. I'd do something in the line of for(i in 1:5){ tmp <- try(fun(data=S[[i]])) results[[i]] <- ifelse(is(tmp,"try-error"),NA,tmp) } Alternatively, you could also use lapply : results <- lapply(S,function(x{ tmp <- try(fun(data=x)) ifelse(is(tmp,"try-error"),NA,tmp) })> > 2) After some runs to my program, I receive this error message someError in > sink("output.txt") : sink stack is full . How can I solve this problem, as I > want to have results of my program for 1000 datasets.That is because you never empty the sink. add sink() after the last line you want in the file. This will empty the sink buffer to the file. Otherwise R keeps everything in the memory, and that gets too full after a while.> > 3) Using for is the correct way to run my proram for a listSee the lapply solution.> > Thanks alot > > Evgenia > > -- > View this message in context: http://r.789695.n4.nabble.com/Problems-when-Apply-a-script-to-a-list-tp2339403p2339403.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- 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.Meys at Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
Where exactly did you put the sink() statement? I tried it with a 1000 dataframes and I have no problem whatsoever. Cheers Joris On Fri, Aug 27, 2010 at 6:56 AM, <EVGTS at aueb.gr> wrote:> Joris, > thank you very much for your help. > It is very helpful for me. > I still have a problem with sink stack although I put after my last print > result sink(). > I want to ask you another one question. Do you know how can I have at the > sink output file a message for which one set of the list are the exact > results. When I use for instead of apply I had > cat("\n***********************\nEstimation \n********************\nDataset > Sim : ", > ? ? ? ? ?i ) > Thank you in advance > Evgenia > Joris Meys writes: >> >> Answers below. >> On Thu, Aug 26, 2010 at 11:20 AM, Evgenia <evgts at aueb.gr> wrote: >>> >>> Dear users, >>> *******I have a function f to simulate data from a model (example below >>> used >>> only to show my problems) >>> f<-function(n,mean1){ >>> a<-matrix(rnorm(n, mean1 , sd = 1),ncol=5) >>> b<-matrix(runif(n),ncol=5) >>> data<-rbind(a,b) >>> out<-data >>> out} >>> *********I want to simulate 1000 datasets (here only 5) so I use >>> S<-list() >>> for (i in 1:5){ >>> S[[i]]<-f(n=10,mean1=0)} >>> ******I have a very complicated function ?for estimation of a model which >>> I >>> want to apply to Each one of the above simulated datasets >>> fun<-function(data){data<-as.matrix(data) >>> sink(' Example.txt',append=TRUE) >>> ? ? ? ? ?cat("\n***********************\nEstimation >>> \n********************\nDataset Sim : ", >>> ? ? ? ? ? ?i ) >>> d<-data%*%t(data) >>> s<-solve(d) >>> print(s) >>> out<-s >>> out >>> } >>> results<-list() >>> for (i in 1:5){results[[i]]<-fun(data=S[[i]])} >>> >>> My questions are: >>> 1) for some datasets system is computational singular and this causes >>> execution of the for to stop.By this way I have only results until this >>> problem happens.How can I pass over the execution for this step and have >>> results for All other datasets for which function fun is applicable? >> >> see ?try, or ?tryCatch. >> I'd do something in the line of >> for(i in 1:5){ >> ? ? tmp <- try(fun(data=S[[i]])) >> ? ? results[[i]] <- ifelse(is(tmp,"try-error"),NA,tmp) >> } >> Alternatively, you could also use lapply : >> results <- lapply(S,function(x{ >> ? tmp <- try(fun(data=x)) >> ? ?ifelse(is(tmp,"try-error"),NA,tmp) >> }) >>> >>> 2) After some runs to my program, I receive this error message someError >>> in >>> sink("output.txt") : sink stack is full . How can I solve this problem, >>> as I >>> want to have results of my program for 1000 datasets. >> >> That is because you never empty the sink. add sink() after the last >> line you want in the file. This will empty the sink buffer to the >> file. Otherwise R keeps everything in the memory, and that gets too >> full after a while. >>> >>> 3) Using for is the correct way to run my proram for a list >> >> See the lapply solution. >>> >>> Thanks alot >>> Evgenia >>> -- >>> View this message in context: >>> http://r.789695.n4.nabble.com/Problems-when-Apply-a-script-to-a-list-tp2339403p2339403.html >>> Sent from the R help mailing list archive at Nabble.com. >>> ______________________________________________ >>> 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. >> >> >> >> -- >> 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.Meys at Ugent.be >> ------------------------------- >> Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php > > >-- 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.Meys at Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php