fork download
  1. x<-c(0, 2, 4, 8, 12)
  2. y<-c(1, 0.7, 0.4, 0.3, 0.04)
  3. (dat<-data.frame(x, y))
  4.  
  5. (n1<-nls(y~exp(-a*x), dat, start=list(a=1)))
  6. a<-0.1939
  7. f1<-function(x)(exp(-a*x))
  8.  
  9. resid<-function(w){
  10. yhat<-exp(-(w[1]*x)^w[2])
  11. sum((y-yhat)^2)
  12. }
  13.  
  14. (bb<-optim(c(1,1),resid))
  15. (n2<-nls(y~exp(-(b*x)^c), dat, start=list(b=0.1939223, c=0.9872986)))
  16.  
  17. b<-0.1939223
  18. c<-0.9872986
  19. f2<-function(x)(exp(-(b*x)^c))
  20.  
  21. ##plot(dat,type="l",col="blue")
  22. ##curve(f1,add=T,col="red")
  23. ##curve(f2,add=T,col="green")
  24.  
  25. AIC(n1,n2)
  26.  
Success #stdin #stdout 0.33s 22840KB
stdin
Standard input is empty
stdout
   x    y
1  0 1.00
2  2 0.70
3  4 0.40
4  8 0.30
5 12 0.04
Nonlinear regression model
  model:  y ~ exp(-a * x) 
   data:  dat 
     a 
0.1939 
 residual sum-of-squares: 0.01518

Number of iterations to convergence: 5 
Achieved convergence tolerance: 3.662e-06 
$par
[1] 0.1939223 0.9872986

$value
[1] 0.01515266

$counts
function gradient 
      77       NA 

$convergence
[1] 0

$message
NULL

Nonlinear regression model
  model:  y ~ exp(-(b * x)^c) 
   data:  dat 
     b      c 
0.1939 0.9873 
 residual sum-of-squares: 0.01515

Number of iterations to convergence: 2 
Achieved convergence tolerance: 2.729e-06 
   df        AIC
n1  2 -10.798190
n2  3  -8.805701