Re: [R] MASS fitdistr with plyr or data.table?

From: Jorge Ivan Velez <jorgeivanvelez_at_gmail.com>
Date: Wed, 27 Apr 2011 17:14:07 -0400

Hi Justin,

One way of doing it is using a combination of tapply() and sapply() as follows:

# data
set.seed(144)
weib.dist<-rweibull(10000,shape=3,scale=8) weib.test.too<-data.frame(cbind(1:10,weib.dist)) names(weib.test.too)<-c('site','wind_speed')

# results
require(MASS)
out <- with(weib.test.too, tapply(wind_speed, site, function(x) fitdistr(x, 'weibull')))
estimates <- t(sapply(out, "[[", 1))
SDs <- t(sapply(out, "[[", 2))
estimates

SDs

HTH,
Jorge

On Wed, Apr 27, 2011 at 4:55 PM, Justin Haynes <> wrote:

> I am trying to extract the shape and scale parameters of a wind speed
> distribution for different sites. I can do this in a clunky way, but
> I was hoping to find a way using data.table or plyr. However, when I
> try I am met with the following:
>
> set.seed(144)
> weib.dist<-rweibull(10000,shape=3,scale=8)
> weib.test<-data.table(cbind(1:10,weib.dist))
> names(weib.test)<-c('site','wind_speed')
>
> fitted<-weib.test[,fitdistr(wind_speed,'weibull'),by=site]

>
> Error in class(ans[[length(byval) + jj]]) = class(testj[[jj]]) :
> invalid to set the class to matrix unless the dimension attribute is
> of length 2 (was 0)
> In addition: Warning messages:
> 1: In dweibull(x, shape, scale, log) : NaNs produced
> ...
> 10: In dweibull(x, shape, scale, log) : NaNs produced
>
> (the warning messages are normal from what I can tell)

>
> or using plyr:
>
> set.seed(144)
> weib.dist<-rweibull(10000,shape=3,scale=8)
> weib.test.too<-data.frame(cbind(1:10,weib.dist))
> names(weib.test.too)<-c('site','wind_speed')
>
> fitted<-ddply(weib.test.too,.(site),fitdistr,'weibull')
>
> Error in .fun(piece, ...) : 'x' must be a non-empty numeric vector
>
> those sound like similar errors to me, but I can't figure out how to
> make them go away!

>
> to prove I'm not crazy:

>
> fitdistr(weib.dist,'weibull')$estimate
> shape scale
> 2.996815 8.009757
> Warning messages:
> 1: In dweibull(x, shape, scale, log) : NaNs produced
> 2: In dweibull(x, shape, scale, log) : NaNs produced
> 3: In dweibull(x, shape, scale, log) : NaNs produced
> 4: In dweibull(x, shape, scale, log) : NaNs produced
>
> Thanks
>
> Justin
>
> ______________________________________________
> 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.
>

        [[alternative HTML version deleted]]



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 Wed 27 Apr 2011 - 21:20:55 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 Wed 27 Apr 2011 - 21:50:34 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