# Least Squares Regression

## Question:

How can I compute a regression line?

The script **regresst.jnl** defines variables "SLOPE", "INTERCEP", "RSQUARE", and"QHAT" given a dependent and independent variable. If you have a variable defined on the X axis, (or you can use RESHAPE to put it on an X axis) then use the script regressx.jnl

regresst.jnl offers the following coaching lines about its inputs and outputs:

... Linear Regression Along the T Axis ... Instructions: Use the LET command to define new variables Define the variable P as your independent (X) variable Define the variable Q as your dependent (Y) variable Results will be variables "SLOPE", "INTERCEP" and "RSQUARE" QHAT will be the regression estimate Note: If "T" is your independent variable then ... "SET GRID Q" after defining Q. ...

Define variables P and Q as the inputs to the script.

yes? USE rainfall.nc yes? LET p = t[gt=rain] yes? LET q = rain yes? SET GRID q yes? GO regresst

Here are our definitions of P and Q, and some of the variables that the script defines. One might want to use QAVE or QVAR, the mean and variance of variable Q.

yes? SHOW VARCreated by DEFINE VARIABLE: >>> Definitions that replace any file variable of same name: P = T[GT=RAIN] Q = RAIN ... PAVE = PMASKED[T=@AVE] QAVE = QMASKED[T=@AVE] PVAR = PPAVE - PAVE*PAVE QVAR = QQAVE - QAVE*QAVE SLOPE = PQVAR / PVAR INTERCEP = QAVE - SLOPE*PAVE QHAT = SLOPE*P + INTERCEP RSQUARE = (PQVAR*PQVAR) / (PVAR*QVAR)

Show the input data as a scatter point, and the regression line

yes? SET VIEW upper yes? PLOT/SYM q yes? PLOT/OVER qhat

Subtract qhat from the variable to get the series, minus its mean and trend.

yes? SET VIEW lower yes? LET/TITLE="rainfall - mean, trend" detrended = rain - qhat yes? PLOT rain, detrended