[R] Really stuck with the nls function!! It's Urgent !!

Adams, Jean jvadams at usgs.gov
Thu May 28 14:36:07 CEST 2015


I can't answer your question, but I can help you get help by re-writing
your code so it's easy for others to see what you're talking about ...

Jean


library(expm)
apinene_modele_prediction <- function(t, theta) {
  x0 = c(100, 0, 0, 0, 0)
  A = matrix(c(
    -(theta[1]+theta[2]), theta[1], theta[2], 0, 0,
    0, 0, 0, 0, 0,
    0, 0, -(theta[3]+theta[4]), theta[3], theta[4],
    0, 0, 0, 0, 0,
    0, 0, theta[5], 0, -theta[5]
    ), 5, 5)
  X = x0
  for (i in t[2:length(t)]) {
    X = c(X, x0 %*% expm(A*i))
  }
  return(X)
}

t = seq(0, 100, by = 2)
theta = c(0.2, 0.2, 0.2, 0.2, 0.2)
nls(y ~ apinene_modele_prediction(t, theta),
  start = list(theta = c(0.2, 0.2, 0.2, 0.2, 0.2)))
nls(y ~ apinene_modele_prediction(t, c(theta, theta, theta, theta, theta)),
  start = list(theta = 0.2))


On Wed, May 27, 2015 at 6:18 PM, oussama belmejdoub <oussa.belm at hotmail.com>
wrote:

> Greetings,
>
> I'm trying to use the nls function in my statistics project but I'm really
> finding lot of difficulties.
>
> I have a function called apinene_modele_prediction that calculates the
> estimations:
> library(expm); #exp of a matrixapinene_modele_prediction <-
> function(t,theta)
> {x0=c(100,0,0,0,0);A=matrix(c(-(theta[1]+theta[2]),theta[1],theta[2],0,0,0,0,0,0,0,0,0,-(theta[3]+theta[4]),theta[3],theta[4],0,0,0,0,0,0,0,theta[5],0,-theta[5]),5,5);X=x0;for
> (i in t[2:length(t)]){X=c(X,x0%*%expm(A*i));}return(X);}
>
> My "t" vector is given by:
> t=seq(0,100,by=2)
> And the real observations "y" ara given to us  in a txt file called
> "data.txt" that I have joined to this message.
>
> So when I try to fit the "theta" in my model starting with:
> theta=c(0.2,0.2,0.2,0.2,0.2)
> And doing:
> theta_appr
> <-nls(y~apinene_modele_prediction(t,theta),start=list(theta=c(0.2,0.2,0.2,0.2,0.2)))
> I always get the ERROR : singular gradient matrix at initial parameter
> estimates
>
> And, when I try:
> nls(y~apinene_modele_prediction(t,c(theta,theta,theta,theta,theta)),start=list(theta=0.2))
> I get the result:Nonlinear regression model  model: y ~
> apinene_modele_prediction(t, c(theta,theta,theta,theta,theta))data:
> parent.frame()theta 0.04403residual sum-of-squares: 219002
>
>
> But I need to have the elements of the theta to be different and not equal.
> Thanks in advance for your help.
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list