[R] Fitting a curve to weibull distribution in R using nls

Therneau, Terry M., Ph.D. therneau at mayo.edu
Wed Oct 14 14:49:59 CEST 2015



On 10/14/2015 05:00 AM, r-help-request at r-project.org wrote:
> I am trying to fit this data to a weibull distribution:
>
> My y variable is:1  1  1  4  7 20  7 14 19 15 18  3  4  1  3  1  1  1
> 1  1  1  1  1  1
>
> and x variable is:1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
> 19 20 21 22 23 24
>

One could always use existing R functions that fit a Weibull regression, instead of 
reinventing the wheel.

library(survival)
y <- scan()
1  1  1  4  7 20  7 14 19 15 18  3
4  1  3  1  1  1
1  1  1  1  1  1

wfit <- survreg(Surv(1:24) ~ 1, weights=y, dist="weibull")
wfit

Call:
survreg(formula = Surv(1:24) ~ 1, weights = y, dist = "weibull")

Coefficients:
(Intercept)
    2.352121

Scale= 0.4130924

Loglik(model)= -351.4   Loglik(intercept only)= -351.4
n= 24


zz <- seq(0, 25, length=100)
plot(zz, dsurvreg(zz, 2.352121, 0.4130924), col=2, type='l', ylim=c(0, .15),
	xlab="Value", ylab="Density")
points(1:24, y/sum(y))

-----
There are a half dozen ways to parameterize a Weibull distribution; the location-scale 
form used by survreg is one of the less common.  See help(survreg) for more information -- 
look at the example near the bottom of the page.

Terry Therneau



More information about the R-help mailing list