在R中拟合饱和增长率模型

时间:2018-08-20 23:41:11

标签: r nonlinear-functions

我有一个响应变量和一个自变量,这些变量在视觉上适合饱和增长率模型。如何在R中拟合这样的模型?谢谢!

1 个答案:

答案 0 :(得分:0)

尝试使用nls函数,但是下次请提供一些示例数据。我使用的是这位同事(https://bscheng.com/2014/05/07/modeling-logistic-growth-data-in-r/)出色的教程中的数据:

library("car"); library("ggplot2")
#Here's the data
mass<-c(6.25,10,20,23,26,27.6,29.8,31.6,37.2,41.2,48.7,54,54,63,66,72,72.2,
        76,75) #Wilson's mass in pounds
days.since.birth<-c(31,62,93,99,107,113,121,127,148,161,180,214,221,307,
                    452,482,923, 955,1308) #days since Wilson's birth
data<-data.frame(mass,days.since.birth) #create the data frame
plot(mass~days.since.birth, data=data) #always look at your data first!

wilson<-nls(mass~phi1/(1+exp(-(phi2+phi3*days.since.birth))),
            start=list(phi1=100,phi2=-1.096,phi3=.002),data=data,trace=TRUE)

#set parameters
phi1<-coef(wilson)[1]
phi2<-coef(wilson)[2]
phi3<-coef(wilson)[3]
x<-c(min(data$days.since.birth):max(data$days.since.birth)) #construct a range of x values bounded by the data
y<-phi1/(1+exp(-(phi2+phi3*x))) #predicted mass
predict<-data.frame(x,y) #create the prediction data frame#And add a nice plot (I cheated and added the awesome inset jpg in another program)
ggplot(data=data,aes(x=days.since.birth,y=mass))+
  geom_point(color='blue',size=5)+theme_bw()+
  labs(x='Days Since Birth',y='Mass (lbs)')+
  scale_x_continuous(breaks=c(0,250,500,750, 1000,1250))+
  scale_y_continuous(breaks=c(0,10,20,30,40,50,60,70,80))+
  theme(axis.text=element_text(size=18),axis.title=element_text(size=24))+
  geom_line(data=predict,aes(x=x,y=y), size=1)

enter image description here