\HeaderA{Regression-gradients}{REGression based CALculus for empirical data}{Regression.Rdash.gradients}
\aliasA{coefDeriv}{Regression-gradients}{coefDeriv}
\aliasA{coefFit}{Regression-gradients}{coefFit}
\aliasA{coefInt}{Regression-gradients}{coefInt}
\aliasA{derivFFT}{Regression-gradients}{derivFFT}
\aliasA{geoGrad}{Regression-gradients}{geoGrad}
\aliasA{integrFFT}{Regression-gradients}{integrFFT}
\aliasA{testFFTcalc}{Regression-gradients}{testFFTcalc}
\aliasA{testReg.cal}{Regression-gradients}{testReg.cal}
\keyword{manip}{Regression-gradients}
\begin{Description}\relax
Use an n-order polynomial fit to model a data series. Ths polynomial
then provides the basis for the calculus: derivations and integrations.
One way of estimating slopes in the terrain or the geostrophic winds
components from sea level pressure fields.

\code{coefFit} fits the coeficients of a power series \deqn{y= a_0 +
a_1 x + a_2 x^2 + a_3 x^3 + ...}{y= a0 + a1 x + a2 x^2 + a3 x^3 + ...}, where \deqn{x = -1,...,+1}{x = -1,...,+1}. Reference: R.E. Benestad (2003) What can present climate models tell us about climate change? Climatic Change Vol 59, 311-332

\code{coefDeriv} computes the first derivative of a power series \deqn{y= a_0 +
a_1 x + a_2 x^2 + a_3 x^3 + ...}{y= a0 + a1 x + a2 x^2 + a3 x^3 + ...} using the results from \code{coefFit}  so that \deqn{dy/dx= a_1  + 2 a_2 x
+ 3 a_3 x^2 + ...}{dy/dx= a1 + 2 a2 x + 3 a3 x^2 +
...}. (Reference:e.g. G. Stephenson (1961), Mathematical methods for
science students, Logman  Scientific \& Technical, p. 86).

\code{coefInt} integrates a series \deqn{y= a_0 + a_1 x + a_2 x^2
+ a_3 x^3 + ...}{y= a0 + a1 x + a2 x^2 + a3 x^3 + ...} using the
results from \code{coefFit} so that \deqn{\int ydx = a_0 x + 1/2 a_1
x^2 + 1/3 a_2 x^3 + 1/4 a_3 x^4 +  ... + const}{integral(y)=a0 x + 1/2 a1
x^2 + 1/3 a2 x^3 + 1/4 a3 x^4 +  ... + const}.

\code{geoGrad} computes gradients of the topography.The topolography data
file can be obtained from \url{http://ferret.pmel.noaa.gov/NVODS/servlets/dataset}.

\code{testReg.cal} is a test-function for \code{coefFit},
\code{coefDeriv} and \code{coefInt}.

\code{derivFFT} uses FFT to compute the first derivative. Anolagous to \code{\LinkA{dX}{dX}}

\code{integrFFT}  uses FFT to integrate a series and is the (pseudo) inverse of
\code{derivFFT}. 

\code{testFFTcalc} is a test function for \code{derivFFT} and \code{integrFFT}.
\end{Description}
\begin{Usage}
\begin{verbatim}
coefFit(y,x=NULL,n=length(y),method="lm")
coefDeriv(y)
coefInt(y,c1=0)
geoGrad(maxhar = 35, fname = "data/etopo5_scandinavia.Rdata",
        x.rng = c(-10, 35), y.rng = c(50, 73), plot = FALSE)
testReg.cal(i.y=240,N=50)
derivFFT(y)
integrFFT(y)
testFFTcalc()
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{y}] A vector.
\item[\code{x}] A vector of index values (e.g. time or longitude).
\item[\code{n}] Number of harmonics to fit
\item[\code{method}] Regression model for fitting the harmonics
\item[\code{c1}] first coefficient: the constant term
\item[\code{maxhar}] number of harmonix (see \code{\LinkA{dX}{dX}})
\item[\code{i.y}] 
\item[\code{N}] 
\item[\code{x.rng}] longitude range
\item[\code{y.rng}] latitude range
\item[\code{fname}] Name of topology file
\item[\code{plot}] Plot the results
\end{ldescription}
\end{Arguments}
\begin{Value}
\end{Value}
\begin{Author}\relax
R.E. Benestad
\end{Author}
\begin{Examples}
\begin{ExampleCode}
## Not run: 
# Polynomial series calculus. (testReg.cal)
  library(clim.pact)
  load("data/etopo5_scandinavia.Rdata")
  y <- ROSE[i.y,]
  a <- coefFit(y,n=N)
  da <- coefDeriv(a)
  a.2 <- coefInt(da,c1=a$coefs[1])
  newFig()
  plot(ETOPO5X,y,type="s",lwd=3,xlab="Longitude (degE)",ylab="m.a.s.l.",
       main=paste("Transect: ",round(ETOPO5Y[i.y],1),"degE"))
  polygon(c(ETOPO5X,ETOPO5X[1320],ETOPO5X[1]),
          c(da$y.deriv/quantile(da$y.deriv,0.9)*quantile(y,0.7),0,0),col="blue")
  lines(ETOPO5X,y,type="s",lwd=4)
  lines(ETOPO5X,a$y.hat,col="red",lty=2,lwd=2)
  lines(ETOPO5X,a.2$y.int,col="steelblue",lty=1)

# FFT-based calculus  (testFFTcalc)

x <- seq(-3,3,length=100)
y <- 3*cos(5*x) + 0.3*sin(18*x) - 1.4*sin(3*x) + 0.15*sin(23*x)
plot(x,y,type="l",lwd=3)
grid()
dydx <- deriv.fft(y)
y2 <- integr.fft(dydx)
lines(x,y2,col="red",lty=2,lwd=2)
grid()
newFig()
plot(x,Im(dydx),type="l",lwd=3)
grid()
lines(x,rep(0,length(x)),col="grey")  
## End(Not run)
\end{ExampleCode}
\end{Examples}

