绘制具有分类变量和独立参数值的非线性(NLS)模型的单独曲线

发布于 2025-02-13 22:12:37 字数 1680 浏览 2 评论 0原文

我有一个年龄和长度的数据集,以及一些分类变量,包括性别和位置(2级因子)。我使用nls()

gompertz <- nls(Length~a*exp(-b*exp(-c*age)), 
                     data=df, 
                     start=list(a=155,b=0.4, c=0.1)) 

但也一直在努力包含一个分类变量 - 即我想比较2个位置之间的增长率。这里提出了一种解决方案:

https://stats.stackexchange.com/questions/63226/non-linear-modelling-with-several-variables-including-a-categorical-variable- variable?newreg = b3e4387021fe41021fe41021fe4108ad7407455bee2bee2bee2e2e2e2e370a

而且该模型有效 - 我对位置因子的两个级别都获得了单独的A,B和C参数估计。但是,我不知道该如何绘制这个问题 - 我想绘制我的原始数据(按年龄为age),其中2个单独的预测曲线,1对于单独的参数估计值的位置值1。

我的模型如下:

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))

可以使用以下方式生成样本数据:

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)))

TIA!

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:

https://stats.stackexchange.com/questions/63226/non-linear-modelling-with-several-variables-including-a-categorical-variable?newreg=b3e4387021fe4108ad74075bee2e370a

and this model works - I get separate a, b and c parameter estimates for both levels of my location factor. However I do not know how to go about plotting this - I want to plot my raw data (length by age) which 2 separate prediction curves, 1 for each value of location with the separate parameter estimates.

my model is as follows:

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))

sample data can be generated using:

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)))

TIA!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还在原地等你 2025-02-20 22:12:41

怎么样:

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")

“”

在2022-07-06创建的

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)

他不在意 2025-02-20 22:12:39

首先运行NLS。我们可以使用下标来获取每个位置的单独参数值。然后以拟合值增强DF并将位置转换为因子,以便我们可以运行GGPLOT2。

st <- list(a = c(155, 155), b = c(0.4, 0.4), c = c(0.1, 0.1))
fo <- Length ~ a[location] * exp(-b[location] * exp(-c[location] * age))
fm <- nls(fo, df, start = st)

library(ggplot2)

df2 <- transform(df, fitted = fitted(fm), location = factor(location))
ggplot(df2, aes(age, Length, col = location)) + 
  geom_point() + 
  geom_line(aes(y = fitted))
  

注意

问题中的输入由于使用而无法重现
没有设置的随机数,所以我们使用了此。

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)))

First run nls. We can use subscripts to get separate parameter values for each location. Then augment df with the fitted values and convert location to factor so that we can run ggplot2.

st <- list(a = c(155, 155), b = c(0.4, 0.4), c = c(0.1, 0.1))
fo <- Length ~ a[location] * exp(-b[location] * exp(-c[location] * age))
fm <- nls(fo, df, start = st)

library(ggplot2)

df2 <- transform(df, fitted = fitted(fm), location = factor(location))
ggplot(df2, aes(age, Length, col = location)) + 
  geom_point() + 
  geom_line(aes(y = fitted))
  

screenshot

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)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文