Assign Weibull densities within groups of dataset

91 views Asked by At

I am trying to create a new column on my data.frame (df) with values of Weibull densities.

I need to assign densities for vector of quantiles (w) taking into account groups in this df.
The subsets are delimited by the variable "plot".

Each plot has specific Weibull parameters in which the densities should be originated from.
The parameters are stored in df_2.

Reproducible example:

set.seed(25)

w = rweibull(1200,10,28)
plot = data.frame(c(rep.int(1,300),rep.int(2,300),rep.int(3,300),rep.int(4,300)))
   names(plot)[1] = c("plot")

df = cbind(plot,w)

df_2=data.frame(cbind(c(1,2,3,4),c(28,27,26,25),c(9,9.5,8,7)))
   names(df_2)[1:3] = c("plot","scale","shape")

I tried to adapt a code from hadley's answer here, but did not succeed.

library(plyr)
weibull_density <- ddply(df, "plot", function(x) {
  data.frame(
    density = dweibull(df$w, scale=df_2$scale, shape=df_2$shape)
  )
})

nrow(weibull_density)
[1] 4800

It returns a data.frame with 4800 rows (I was expecting 1200).

I also took a look in examples provided in ?ddply help page, but could not figure out how to adapt it to this situation.

2

There are 2 answers

0
Sven Hohenstein On BEST ANSWER

You obtain 4 values because df_2 has 4 rows. You need to tell R to use, e.g., the first row of df_2 for the w values for plot == 1.

The following code produces the expected output:

weibull_density <- transform(df, 
  density = as.vector(sapply(unique(plot), function(x) 
    dweibull(w[plot %in% x], scale = df_2$scale[x], shape = df_2$shape[x]))))
1
Stephen Henderson On

I think this maybe the simplest??

> df3=merge(df, df_2)
> res=mapply(dweibull, x=df3$w, shape=df3$shape, scale=df3$scale)

> head(res)
[1] 0.11900795 0.09575625 0.09021534 0.04742028 0.08339647 0.01091331

> length(res)
[1] 1200

maybe???