Using xvgr for model fitting
This page briefly explains how to fit a 1-D linear or non-linear model
using the software xvgr.
First, download the file test.dat. This file
has 3 (white space-separated) columns of data. You can load the data
into xvgr when starting the tool:
machine% xvgr -nxy test.dat &
The option -nxy tells xvgr
to accept data with two or more 'y' values per 'x' value (i.e. 2 or
more columns).
The graph doesn't look too interesting to start with, as the 2 datasets
are scaled very differently.
Let's 'ignore' the second dataset for the moment. Switch it off via:
Edit->Set
Operations -> De-activate
select set 1 (the 2nd set) and click 'Apply'. Now
click the button 'AS'
(Autoscale). The first column of data should now show as a graph. You
can re-activate dataset 1 in a similar fashion to the way it was
de-sctivated.
1. Polynomial Model Fitting
Select:
Edit ->
Transformations -> Regression
Choose dataset 0. Select a cubic (as the degree of the polynomial we
require). A window then pops up with the parameters of the fitted
function:
y = A[0] + A[1] x + A[2] x2 + A[3] x3
Some basic statistics on the fitting are also provided, such as the R2
Value to indicate the quality of the fit (essentially proportion of
variance explained).
2. Non-Linear Model Fitting
A polynomial model is often not appropriate. In such cases, you can
define your own function in xvgr.
Select:
Edit ->
Transformations -> Non-linear curve fitting
and type in the equation you want to fit. For example, try:
y = A0 * (1 - exp(-A1 * x)) + A2
Set the number of parameters you have used in the check box, put in
some initial guesses for the inversion, then click Apply.
The fitted model parameters will appear in the relevant column/rows.
Unfortunately, this tool does not give you a measure of goodness of
fit, so it is generally easiest to apply the defined equation outside
of this tool and calculate e.g RMSE using gawk.
e.g.:
machine% gawk < test.dat
'{x=$1;y=$2;Y=A0*(1-exp(-A1*x))+A2;D=y-Y;sum+=D*D;}
END{mse=sum/(1.*NR);print "rmse = ",sqrt(mse)}' A0=13.234717
A1=0.367448 A2=714.332229
rmse = 0.0589398