[R] MASS fitdistr call in plyr help!

From: Justin Haynes <jtor14_at_gmail.com>
Date: Fri, 22 Apr 2011 17:26:04 -0700


I have a set of wind speeds read at different locations. The data is a data frame with two columns: site and wind speed. I want to split the data on site and call a function to find the shape and scale parameters of a weibull distribution fit.

The end result is a plot with x-axis = shape and y-axis = scale. Currently my code looks like:

  fit_wind_speed<-function(x){
    x<-replace(x,x<=0,0.0001)

	  temp<-fitdistr(na.exclude(x[,1]),"weibull")
  	l<-length(names(x))
	  for(i in 1:l){
		  temp[i]<-(fitdistr(na.exclude(x[,i]),"weibull"))
		}
	  temp

}

  wind_speed_wide_dataframe<-function(x){     mini<-min(x$site)
    maxi<-max(x$site)
    ws.plot<-as.matrix(subset(x,site==mini,select=(wind_speed)))     row.names(ws.plot)<-NULL
    for(i in (mini+1):maxi){

      temp<-as.matrix(subset(x,site==i,select=(wind_speed)))
      row.names(temp)<-NULL
      ws.plot<-add.col(ws.plot,temp)

    }
    as.data.frame(ws.plot)
}

ws.plots<-wind_speed_wide_dataframe(dataset[,c(1,3)]) names(ws.plots)<-c(min(dataset$site):max(dataset$site)) fit<-fit_wind_speed(ws.plots)
names(fit)<-names(ws.plots)

l<-length(fit)
i<-1:l
j<-1:2

temp2<-data.frame(1:l,2)
temp<-data.frame(names(fit),2)
for(i in 1:l){temp<-data.frame(fit[i])}
for(i in 1:l){temp[i]<-data.frame(fit[i])}
for(i in 1:l){temp2[i,j]<-temp[j,i]}

names(temp2)<-c("shape","scale")

Id like to combine the two functions into one plyr call, but I can't figure out how it would work! If there is a better package than MASS i'm all ears for that too.

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. Received on Sat 23 Apr 2011 - 00:30:56 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 Sat 23 Apr 2011 - 00:40:33 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