Diff of /inst/nls_example.m [000000] .. [afe5a1]  Maximize  Restore

Switch to unified view

a b/inst/nls_example.m
1
## Copyright (C) 2006  Michael Creel <michael.creel@uab.es>
2
##
3
## This program is free software; you can redistribute it and/or modify
4
## it under the terms of the GNU General Public License as published by
5
## the Free Software Foundation; either version 2 of the License, or
6
## (at your option) any later version.
7
##
8
## This program is distributed in the hope that it will be useful,
9
## but WITHOUT ANY WARRANTY; without even the implied warranty of
10
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
## GNU General Public License for more details.
12
##
13
## You should have received a copy of the GNU General Public License
14
## along with this program; if not, write to the Free Software
15
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
## Example to show how to use NLS
18
19
# Generate data
20
n = 100; # how many observations?
21
22
# the explanatory variables: note that they have unequal scales
23
x = [ones(n,1) rand(n,2)];
24
theta = 1:3; # true coefficients are 1,2,3
25
theta = theta';
26
27
lambda = exp(x*theta);
28
y = randp(lambda); # generate the dependent variable
29
30
# example objective function for nls
31
function [obj_contrib, score] = nls_example_obj(theta, data, otherargs)
32
  y = data(:,1);
33
  x = data(:,2:columns(data));
34
  lambda = exp(x*theta);
35
  errors =  y - lambda;
36
  obj_contrib = errors .* errors;
37
  score = "na";
38
endfunction
39
40
41
####################################
42
# define arguments for nls_estimate #
43
####################################
44
45
# starting values
46
theta = zeros(3,1);
47
48
# data
49
data = [y, x];
50
51
# name of model to estimate
52
model = "nls_example_obj";
53
modelargs = {}; # none required for this obj fn.
54
55
56
# controls for bfgsmin
57
control = {50,1,1,1};
58
59
printf("\n\NLS estimation example\n");
60
[theta, obj_value, convergence] = nls_estimate(theta, data, model, modelargs, control);

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks