Skip to content

nls model singular gradient matrix at initial parameter estimates

3 messages · oussama belmejdoub, Adams, Jean

#
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 got 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 got the result: Nonlinear regression model  model: y ~ apinene_modele_prediction(t, c(theta, theta, theta, theta,     theta))   data: parent.frame()  theta0.04403 residual 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. 		 	   		  
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: data.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20150527/37052351/attachment.txt>
#
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. 		 	   		  
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: data.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20150527/527a3c21/attachment.txt>
#
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: