[R] Save a list of list and search for values

Alaios alaios at yahoo.com
Fri Feb 27 14:09:47 CET 2015


Sorry Jim I forgot to reply on what I am trying to do.I  have many data sets that contain some numbers: I am trying to fit those with a mixture distribution. For that I am using the mix function that returns me back the fitted parameters and the chi square (which is a first performance indicator).After all the fits are completed and gathered on the big list, I would like to see which are the fits with the better chi square numbers and what are their typical numbers. After I see that results I would have to think more on how to proceed
RegardsAlex
 

     On Friday, February 27, 2015 1:58 PM, Alaios <alaios at yahoo.com> wrote:
   

 Hi,thanks all for the answer.I am using mclapply to call the lapply many times as needed. My function returns only a value if the fit is succesful.For testing if the fit is sucessfuly my code works like that
fitcass1<-tryCatch(mix(mixdat=mydataOnVector,mixpar=params,dist=distribution),error=function(e) list(e,"Error"))
if (fitcass1[[2]]=="Error"){
                                              print(sprintf("error at fitting gamma distribution with %s periods. Mean %f %f Sd %f %f",flag,mean1,mean2,sd1,sd2))

                                            }else{ 
(code trunctated)... where I do some plots

so at the end of the function the code looks like
if (fitcass1[[2]]!="Error")
                                        return(fitcass1)

then I am calling the function above with
keeptheBigListAsJimSuggested<-mclapply(expandMeanSigmaOn_list,callFunctionAbove,mydataOnVector=mydataOnVector,filename=filename,mc.cores=64)

If I am not wrong that would work. I will try later after my code stops executing. 
Any more comments on this?
RegardsAlex

 

     On Thursday, February 26, 2015 3:39 PM, jim holtman <jholtman at gmail.com> wrote:
   

 You store it as a list of lists and can then use the lapply function
to navigate for values.

result <- lapply(1:10000, function(x){
    mix(param[x])  # whatever your call to 'mix' is with some data
})





Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Thu, Feb 26, 2015 at 9:27 AM, Alaios via R-help <r-help at r-project.org> wrote:
> Dear all,in my code I am using the mix() function that returns results in a list. The result looks like
> List of 10
>  $ parameters  :'data.frame':  2 obs. of  3 variables:
>  ..$ pi  : num [1:2] 0.77 0.23
>  ..$ mu  : num [1:2] -7034 162783
>  ..$ sigma: num [1:2] 20235 95261
>  $ se          :'data.frame':  2 obs. of  3 variables:
>  ..$ pi.se  : num [1:2] 0.0423 0.0423
>  ..$ mu.se  : num [1:2] 177 12422
>  ..$ sigma.se: num [1:2] 1067 65551
>  $ distribution: chr "norm"
>  $ constraint  :List of 8
>  ..$ conpi  : chr "NONE"
>  ..$ conmu  : chr "NONE"
>  ..$ consigma: chr "NONE"
>  ..$ fixpi  : NULL
>  ..$ fixmu  : NULL
>  ..$ fixsigma: NULL
>  ..$ cov    : NULL
>  ..$ size    : NULL
>  $ chisq      : num 28
>  $ df          : num 5
>  $ P          : num 3.67e-05
>  $ vmat        : num [1:5, 1:5] 1.79e-03 -3.69e-01 -1.17e+02 2.95e+01 -2.63e+03 ...
>  $ mixdata    :Classes ‘mixdata’ and 'data.frame':    11 obs. of  2 variables:
>  ..$ X    : num [1:11] 1e+04 2e+04 3e+04 4e+04 5e+04 6e+04 7e+04 8e+04 9e+04 1e+05 ...
>  ..$ count: int [1:11] 993 137 82 30 21 5 7 14 21 2 ...
>  $ usecondit  : logi FALSE
>  - attr(*, "class")= chr "mix"
>
> In my code I am trying around 10.000 fit (and each of these fits returns the list above) and I want to keep those in a way that later on I would be able to search inside all the lists.For example I would like to find inside those 10.000 lists which one has the smallest $chisq value. What would be a suitable way to implement that in R? Luckily I am working in a computer with a lot of ram so storing 10.000 lists temporary in memory before saving to disk would not be a problem.
> What would you suggest me?
> RegardsAlex
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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]]



More information about the R-help mailing list