1

Example data with weibull decay for growth:

library(tidyverse)

example weibull

example_df <- data.frame( day = 1:30, growth = dweibull(seq(1, 30, by = 1), shape = 0.1) )

example_df |> ggplot(aes(x = day, y = growth)) + geom_point()

enter image description here

Data looks like this:

example_df |> head()
  day      growth
1   1 0.036787944
2   2 0.018348802
3   3 0.012186100
4   4 0.009104847
5   5 0.007257658
6   6 0.006027639

Each day, growth decreases a little - a decay.

I would like to predict decay on a given day. Here's where I get confused.

Suppose I'm asked what decay is on day 20? Or day 25? How can I use a weibull regression to tell me? Tried something:

library(fitdistrplus)
fit.weibull <- fitdist(example_df$growth, distr = "weibull", method = "mle", lower = c(0, 0))

fit.weibull |> summary() Fitting of the distribution ' weibull ' by maximum likelihood Parameters : estimate Std. Error shape 0.962954378 NA scale 0.004707553 NA Loglikelihood: 130.1494 AIC: -256.2989 BIC: -253.4965 Correlation matrix: [1] NA

This seems to fit a weibull curve to my data. But I also need to 'tell it' that growth is dependent on day.

I looked at survreg and survival packages but they want binary 1/0 data for survived/not survived. For my problem, I just want to associate day with growth using a weibull to be able to predict growth on a given day index.

How can I use a weibull to predict growth on a given day?

Doug Fir
  • 1,568
  • 1
  • 19
  • 36
  • 1
    It is difficult to determine what your actual problem is because you seem to be confusing the formula for a Weibull density with a Weibull distribution. Could you please tell us, in non-technical terms, what your data mean and what you are trying to learn about them? – whuber Jan 14 '22 at 17:49
  • Hi @whuber let me try here. We have an app and want to model revenue growth. Revenue grows by a decreasing amount each day, it looks similar to the plot I generated above. So, I would like to use a weibull regression model in order to predict growth on new installs. E.g. if 100 people install our app on monday and in total they spent $1K on day 1. How much revenue would they spend on day 2, day, 3, day 10, etc? Can I use a weibull in this way? Let me know if it's still unclear? I basically wnat to pass a data frame to a weibul regression with 2 fields: Growth Rate and Day – Doug Fir Jan 14 '22 at 17:57
  • 1
    A graph that conforms to the formula for a Weibull density is not a Weibull regression model! You ought to back up quite a bit and consider what a reasonable form for the decay might be--exponential, power, something else. Then, by considering how data might vary, you can provisionally select a form of model that is suitable for their conditional variations. That raises a lot of questions, but I doubt any of them would be correctly answered by making your software work. – whuber Jan 14 '22 at 18:00
  • I just want to know if it's possible to tell a weibull regression that growth is a function of day. You have answered other questions and pointed me elsewhere. Is it possible to use a weibull regression to model growth as a function of day? – Doug Fir Jan 14 '22 at 18:32
  • 2
    You won't do it with fitdist! That's not performing a regression: it's estimating a (univariate) distribution function. You need software to perform nonlinear regression; but what kind of nonlinear regression and which software will depend on the conditional distribution you assume for the model. – whuber Jan 14 '22 at 19:25
  • OK... in this case I want to try a weibull. My real data may or may not be weibulll shaped but it looks like it when I plot. Is there an r package that would let me pass a dataframe to fit a weibull where growth ~ day? The packages I did see use Surv objects which expect 0/1 event data. Rather than 0/1 event data I want to pass a vector that just shows a decreasing growth rate. Is there an R package out there for this>? – Doug Fir Jan 14 '22 at 19:29
  • 1
    With some diffidence--because I suspect it isn't truly applicable for your data--I can refer you to the build-in nls function. Here on CV we have many worked examples of nonlinear regression, with R code. – whuber Jan 14 '22 at 19:31
  • Could you show me how? Tried: nls(GrowthRate ~ TENURE, data = .mydf, algorithm = 'weibull') which errors with "'arg' should be one of “default”, “plinear”, “port”" Note, it's fine if I'm 'wrong' with using a weibull. It's totally fine. – Doug Fir Jan 14 '22 at 19:50
  • Also tried nls(GrowthRate ~ SSweibull(TENURE, Asym, Drop, lrc, pwr), data = mydf) but that gives error "singular gradient" – Doug Fir Jan 14 '22 at 20:17

1 Answers1

1

I think you are looking for something like this:

df <- structure(list(day = 1:6, 
                     growth = c(0.036787944, 0.018348802, 0.0121861, 0.009104847, 0.007257658, 0.006027639)), 
                class = "data.frame", row.names = c(NA, -6L))

#fit the model model<-nls(growth ~ aexp(-bday), data=df, start=list(a=1, b=0.1))

#Set up the prediction test <- data.frame(day=1:10) test$y<-predict(model, test)

#Plot plot(df$day, df$growth, ylim=c(0, 0.04), xlim=c(0, 10)) lines(test$day, test$y, col="blue")

Here we are using the simple exponential decay formula $growth = a*e^{-b*day}$. If this fit is not satisfactory, then maybe try a polynomial or another function of your choosing.

Dave2e
  • 1,651