[R] MASS fitdistr call in plyr help!
Justin Haynes
jtor14 at gmail.com
Sat Apr 23 02:26:04 CEST 2011
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
More information about the R-help
mailing list