我试图找到微分方程组的初始值。我的参数估计返回一些初始值的负值,但所有这些值必须大于或等于0.最重要的是,我想指定初始条件应如下:
A>0
B>0<1
C>0<1
D>0<1
E=0
F=0
G>0<5
H>0<100
获得的所有结果应为阳性(浓度)。 我该如何引入约束?我发现了一些关于如何在使用optim时使用它的信息,但是我无法找到与我的问题相关的任何信息。
任何帮助将不胜感激。
Malgosia
library(ggplot2)
library(reshape2)
library(deSolve)
library(minpack.lm)
time<-seq(0, 5, by=1)
P4=c(0.018,0.028,0.201,0.888,0.934,2.044)
E2=c(0.355,0.28,0.665,0.995,0.934)
FSH=c(0.408,0.226,0.126,0.224,0.123)
signal<-as.data.frame(list(time=time,P4))
input<-approxfun(P4,rule=2)
df<-data.frame(time,E2,FSH)
df
names(df)=c("time","E2","FSH")
#plot data
tmp=melt(df,id.vars=c("time"),variable.name="species",value.name="conc")
ggplot(data=tmp,aes(x=time,y=conc,color=species))+geom_point(size=4)
#rate function
rxnrate=function(t,c,parms){
#c is the concentration of species
#derivatives dc/dt are computed below
P4<-input(t)
r=rep(0,length(c))
r[1]<-12.84*1/(1+(P4/5)^5)*((c["G"]/3)^10)/(1+(c["G"]/3)^10)-2.14*c["A"];
r[2]<-75*(((c["A"]/5)^10)/(1+(c["A"]/5)^10))-8.56*c["B"];
r[3]<-12.84*(1/(1+(c["H"]/2)^2))*(1/(1+(c["G"]/10)^10))+1*(c["A"]/1)^1)/(1+(c["A"]/1)^1))-2.14*c["C"];
r[4]<-0.0107*c["C"]+0.321*c["C"]*c["D"]- 0.749*c["D"];
r[5]<-0.749*c["D"]- 0.749*c["E"]+0.214*c["C"]*c["E"]^2;
r[6]<-0.749*c["E"]-0.749*c["F"]+0.214*c["B"]*c["F"]^2;
r[7]<-0.0107 + 2.14*c["E"] + 10.7*c["F"]-1.07*c["G"];
r[8]<-0.0107+3*c["E"]+ 3.21*c["F"]+3.21*c["G"]-1.07*c["H"];
return(list(r))
}
cinit<-c(A=0.3947,B=0.40727,C=0.408,D=0.17828,E=0,F=0.05,G=0.355,H=0.9);
t=df$time;
out=ode(y=cinit,times=t,func=rxnrate)
head(out)
plot(out)
ssq=function(myparms){
#initial concentration
cinit=c(A=myparms[1],B=myparms[2],C=myparms[3],D=myparms[4],E=myparms[5],F=myparms[6],G=myparms[7],H=myparms[8])
cinit=c(A=unname(myparms[1]),B=unname(myparms[2]),C=unname(myparms[3]),D=unname(myparms[4]),E=unname(myparms[5]),F=unname(myparms[6]),G=unname(myparms[7]),H=unname(myparms[8]))
print(cinit)
#time points for which conc is reported
#include the points where data is available
t=c(seq(0,5,1),df$time)
t=sort(unique(t))
#parameters from the parameters estimation
#solve ODE for a given set of parameters
out=ode(y=cinit,times=t,func=rxnrate)
#Filter data that contains time points
outdf=data.frame(out)
outdf=outdf[outdf$time%in% df$time,]
#Evaluate predicted vs experimental residual
preddf=melt(outdf,id.var="time",variable.name="species",value.name="conc")
expdf=melt(df,id.var="time",variable.name="species",value.name="conc")
ssqres=preddf$conc-expdf$conc
return(ssqres)
}
# parameter fitting using levenberg marquart
#initial guess for parameters
myparms=c(A=0.6947,B=0.4072,C=0.408,D=0.2,E=0,F=0,G=0.355,H=0.9)
#fitting
fitval=nls.lm(par=myparms,fn=ssq)
#summary of fit
summary(fitval)