Weighted Linear Least Squares

From Powers Wiki

This page details how to use the wlls program to calculate variance-weighted linear least squares regression parameters from experimental data.

Example output plot of weighted linear least squares fit to data performed by wlls

Export the data into a text file

wlls is picky when it comes to input file formatting. Make sure to follow these instructions to the letter. In short, the input format for wlls is ASCII text, with a single space separating each field. The first column (field) corresponds to the x-axis data, which is assumed to be known with zero error. All remaining columns are y-axis data values. The number of y-axis values does not have to match for each x-axis point (i.e. the number of fields/columns per row does not have to be constant).

Exporting from Excel

Say you have a (gasp!) spreadsheet containing the values you need to fit. The file File:Wlls-example.xlsx shows an example dataset. Notice that the values are left-justified!! This is very important.

You can simply save the excel data as Text (Tab Delimited) *.txt. Once the file is saved, convert all tabs to spaces with the following linux command:

sed -e 's,\t, ,g' -i wlls-example.txt

Where wlls-example.txt is the text filename saved from excel. It should look similar to the following data: (Example final text file at File:Wlls-example.txt)

10 22 23 21 20 20 
20 43 45 38 35 40 41
40 80 82 86 70  
50 110 100 95 98 102 
80 180 120 240   
100 250 300 280 320  

Running the program

To run wlls, just execute the following linux command on the cluster:

wlls < wlls-example.txt

Where, again, wlls-example.txt is the exported, fixed text file you want to fit a line to. The program will respond with output akin to this if everything worked:

[ y = ax + b ]
  a  = 1.909741152913573492e+00
  b  = 1.434756878848905348e+00
  r2 = 0.998381952730418742

And there you have it!

Plotting the output

If you want to look at the fit line in the context of the data points, you can use gnuplot as follows:

/usr/bin/gnuplot
gnuplot> unset key
gnuplot> plot "wlls-example.txt" using 1:2 with points,
              "" using 1:3 with points,
              "" using 1:4 with points,
              "" using 1:5 with points,
              1.909741152913573492e+00 * x + 1.434756878848905348e+00 with lines