I have a dataset of age and length, plus some categorical variables including sex and location (2 level factor). I have fit a Gompertz model to this, using nls()
:
gompertz <- nls(Length~a*exp(-b*exp(-c*age)),
data=df,
start=list(a=155,b=0.4, c=0.1))
but have been struggling to include a categorical variable as well - i.e. I want to compare the growth rates between 2 locations. A solution has been suggested here:
Note
The input in the question is not reproducible due to the use of random numbers without set.seed so we used this.
set.seed(123)
ages<- runif(100, 0, 22) #ages 0-22
#parameters for model
a1<-153
b1<-0.51
c1<-0.53
a2<-147
b2<-0.45
c2<-0.43
#generate length with error normally distributed
length1 <- (a1*exp(-b1*exp(-c1*ages))) rnorm(100, mean=0, sd=5)
length2 <- (a2*exp(-b2*exp(-c2*ages))) rnorm(100, mean=0, sd=5)
df <- data.frame(Length=c(length1, length2), age=rep(ages, 2),
location=c(rep(1,100), rep(2,100)))
CodePudding user response:
How about this:
library(ggplot2)
ages<- runif(100, 0, 22) #ages 0-22
#parameters for model
a1<-153
b1<-0.51
c1<-0.53
a2<-147
b2<-0.45
c2<-0.43
#generate length with error normally distributed
length1 <- (a1*exp(-b1*exp(-c1*ages))) rnorm(100, mean=0, sd=5)
length2 <- (a2*exp(-b2*exp(-c2*ages))) rnorm(100, mean=0, sd=5)
df<-data.frame(Length=c(length1, length2), age=rep(ages, 2), location=c(rep(1,100), rep(2,100)))
gompertz.locs <- nls(formula = Length ~
as.numeric(location==1)*a1*exp(-b1*exp(-c1*age))
as.numeric(location==2)*a2*exp(-b2*exp(-c2*age)),
data = df,
start = list(a1=150,b1=0.5, c1=0.5,
a2=150,b2=0.5, c2=0.5))
a <- coef(gompertz.locs)[c(1,4)]
b <- coef(gompertz.locs)[c(2,5)]
c <- coef(gompertz.locs)[c(3,6)]
df$fit <- a[df$location]*exp(-b[df$location]*exp(-c[df$location]*df$age))
ggplot(df, aes(x=age, y=Length, colour=as.factor(location)))
geom_point()
geom_line(aes(y=fit))
theme_classic()
labs(colour="Location")
Created on 2022-07-06 by the reprex package (v2.0.1)