P.Chakytei
P.Chakytei

Reputation: 414

Fit “multimodal” lognormal distributions using R

My question is similar to the one here but I want to do it in R. The data frame is

x<-c(0.35,0.46,0.60,0.78,1.02,1.34,1.76,2.35,3.17,4.28,5.77,7.79,10.50,14.20,19.10,25.80)
y<-c(32.40,43.00,37.20,26.10,17.40,14.00,19.90,36.90,48.60,55.30,64.60,70.20,63.90,47.60,22.70,10.30)

df<-data.frame(x,y)
plot(df,log='xy')

enter image description here

When plotted here is how the data look. There is one mode around 0.5 and another mode around 8 in the units of the x-scale.

How do I fit "multimodal" lognormal distributions to such data (In this case with 2 curves)? Here's what I have tried. Any help or direction to solve it is greatly appreciated.

ggplot(data=df, aes(x=x, y=y)) + 
  geom_point() + 
  stat_smooth(method="nls", 
              formula=y ~ a*dlnorm(x, meanlog=8, sdlog=2.7),
              method.args = list(start=c(a=2e6)), 
              se=FALSE,color = "red", linetype = 2)+
  scale_x_log10()+
  scale_y_log10()

Upvotes: 2

Views: 666

Answers (1)

jay.sf
jay.sf

Reputation: 72994

I'm assuming you want nls. You may consider two modes by defining two parameters in your equation, say a and b. Define for both the start=ing values. (Note, that I just guessed all the values at this time.)

fit <- nls(y ~ a*dlnorm(x, meanlog=.5, sdlog=.5) + b*dlnorm(x, meanlog=8, sdlog=2.7),
           data=df1, start=list(a=1, b=1))
summary(fit)
# Formula: y ~ a * dlnorm(x, meanlog = 0.5, sdlog = 0.5) + b * dlnorm(x, 
#     meanlog = 8, sdlog = 2.7)
# 
# Parameters:
#   Estimate Std. Error t value Pr(>|t|)    
# a   -81.97      16.61  -4.934  0.00022 ***
# b 30695.42    2417.90  12.695 4.53e-09 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 11.92 on 14 degrees of freedom
# 
# Number of iterations to convergence: 1 
# Achieved convergence tolerance: 4.507e-07

fitted() already gives you the fitted values for y along the x values of your data frame.

fitted(fit)
# [1] 45.56775 44.59130 38.46212 27.34071 15.94205 12.76579 21.31640
# [8] 36.51385 48.68786 53.60069 53.56958 51.40254 48.41267 44.95541
# [15] 41.29045 37.41424
# attr(,"label")
# [1] "Fitted values"

You could also use predict() for this.

stopifnot(all.equal(predict(fit), as.numeric(fitted(fit))))

However, to get a smoother line you want a prediction (i.e. y values) along a finer set of x values along your x axis.

plot(df1, log='xy')
x.seq <- seq(0, max(df$x), .1)
lines(x=x.seq, y=predict(fit, newdata=data.frame(x=x.seq)), col=2)

enter image description here

A sidenote: Even if this is very common, by naming your data frame df you're using the same name that is used for the density function df() for the F distribution, which may lead to confusion! For this reason I used df1.


Data:

df1 <- structure(list(x = c(0.35, 0.46, 0.6, 0.78, 1.02, 1.34, 1.76, 
2.35, 3.17, 4.28, 5.77, 7.79, 10.5, 14.2, 19.1, 25.8), y = c(32.4, 
43, 37.2, 26.1, 17.4, 14, 19.9, 36.9, 48.6, 55.3, 64.6, 70.2, 
63.9, 47.6, 22.7, 10.3)), class = "data.frame", row.names = c(NA, 
-16L))

Upvotes: 1

Related Questions