Reputation: 2077
I have a time series of temperature data as:
ifile.txt
1921 25
1922 25.1
1923 24.2
1924 23.4
1925 24.4
1926 25.1
1927 23.6
1928 25.2
1929 23.9
1930 25.6
I would like to compute the trend for the period 1923-1929.
My algorithm to do it is:
Fit the above points to a line such as
Y = mX + c ; where m is the trend
I could find the trend for whole period, but I can't able to modify my script when selecting for a specific period. Would you please check my script?
awk '/1923/,/1929/ {if (NR==FNR) x[NR] = $1;y[NR] = $2;
sx += x[NR]; sy += y[NR];
sxx += x[NR]*x[NR];
sxy += x[NR]*y[NR];}
END{
det = NR*sxx - sx*sx;
trend = (NR*sxy - sx*sy)/det;
print trend;}' ifile.txt
It is not printing correct value.
The correct value is 0.0679
Upvotes: 1
Views: 174
Reputation: 67497
you can simplify it little bit, you're not counting the matching records. NR
will be total number of lines in the END block.
awk '/1923/,/1929/ {sx+=$1; sy+=$2; c++;
sxx+=$1*$1; sxy+=$1*$2}
END {det=c*sxx-sx*sx;
print det?(c*sxy-sx*sy)/det:"DIV0"}' file
also need to consider denominator being zero. The trend comes up as
0.0678571
Upvotes: 2