Title: | Local Polynomial Regression of State Dependent Covariates in State-Space Models |
---|---|
Description: | Facilitates local polynomial regression for state dependent covariates in state-space models. The functionality can also be used from 'C++' based model builder tools such as 'Rcpp'/'inline', 'TMB', or 'JAGS'. |
Authors: | Christoffer Moesgaard Albertsen [aut, cre] |
Maintainer: | Christoffer Moesgaard Albertsen <[email protected]> |
License: | BSD_2_clause + file LICENSE |
Version: | 0.4.4 |
Built: | 2024-10-25 03:46:31 UTC |
Source: | https://github.com/calbertsen/covafillr |
A Reference Class for Local Polynomial Regression with covafill.
ptr
External pointer to the covafill C++ object
getBandwith()
Get the bandwith.
getDegree()
Get the polynomial degree.
getDim()
Get the dimension of the coordinates.
initialize(coord, obs, h = suggestBandwith(coord, p), p = 3L, ...)
Method to initialize the covafill. coord is a matrix of coordinates, obs is a vector of corresponding observations, h is a vector of bandwiths, and p is the polynomial degree.
predict(coord, se.fit = FALSE)
Predict function value and derivatives with local polynomial regression at coord. If se.fit=TRUE a list is returned with estimates and their standard deviations.
residuals(excludeRadius)
Get 'leave-neighborhood-out' residuals, i.e. local polynomial regression predictions excluding points within excludeRadius subtracted from the observation.
setBandwith(h)
Set the bandwith to h.
getRefClass('covafill') fn <- function(x) x ^ 4 - x ^ 2 x <- runif(2000,-3,3) y <- fn(x) + rnorm(2000,0,0.1) cf <- covafill(coord = x,obs = y,p = 5L) cf$getDim() cf$getDegree() cf$getBandwith() x0 <- seq(-1,1,0.1) y0 <- cf$predict(x0) par(mfrow=c(3,1)) plot(x0,y0[,1], main = "Function") lines(x0,fn(x0)) plot(x0, y0[,2], main = "First derivative") lines(x0, 4 * x0 ^ 3 - 2 * x0) plot(x0, y0[,3], main = "Second derivative") lines(x0, 3 * 4 * x0 ^ 2 - 2) cf$setBandwith(1.0) cf$getBandwith()
getRefClass('covafill') fn <- function(x) x ^ 4 - x ^ 2 x <- runif(2000,-3,3) y <- fn(x) + rnorm(2000,0,0.1) cf <- covafill(coord = x,obs = y,p = 5L) cf$getDim() cf$getDegree() cf$getBandwith() x0 <- seq(-1,1,0.1) y0 <- cf$predict(x0) par(mfrow=c(3,1)) plot(x0,y0[,1], main = "Function") lines(x0,fn(x0)) plot(x0, y0[,2], main = "First derivative") lines(x0, 4 * x0 ^ 3 - 2 * x0) plot(x0, y0[,3], main = "Second derivative") lines(x0, 3 * 4 * x0 ^ 2 - 2) cf$setBandwith(1.0) cf$getBandwith()
A Reference Class for Search Tree Approximated Local Polynomial Regression with covatree.
ptr
External pointer to the covatree C++ object
getDim()
Get the dimension of the coordinates.
initialize(
coord,
obs,
h = suggestBandwith(coord, p),
p = 3L,
minLeft = length(obs)/10,
...
)
Method to initialize the covafill. coord is a matrix of coordinates, obs is a vector of corresponding observations, h is a vector of bandwiths, p is the polynomial degree, and minLeft is the minimum number of observations that will create a sub tree.
predict(coord)
Predict function value and first order derivatives with search tree approximated local polynomial regression at coord.
getRefClass('covatree') fn <- function(x) x ^ 4 - x ^ 2 x <- runif(2000,-3,3) y <- fn(x) + rnorm(2000,0,0.1) ct <- covatree(coord = x,obs = y,p = 5L, minLeft = 50) ct$getDim() x0 <- seq(-1,1,0.1) y0 <- ct$predict(x0) par(mfrow=c(2,1)) plot(x0,y0[,1], main = "Function") lines(x0,fn(x0)) plot(x0, y0[,2], main = "First derivative") lines(x0, 4 * x0 ^ 3 - 2 * x0)
getRefClass('covatree') fn <- function(x) x ^ 4 - x ^ 2 x <- runif(2000,-3,3) y <- fn(x) + rnorm(2000,0,0.1) ct <- covatree(coord = x,obs = y,p = 5L, minLeft = 50) ct$getDim() x0 <- seq(-1,1,0.1) y0 <- ct$predict(x0) par(mfrow=c(2,1)) plot(x0,y0[,1], main = "Function") lines(x0,fn(x0)) plot(x0, y0[,2], main = "First derivative") lines(x0, 4 * x0 ^ 3 - 2 * x0)
Get CXXFLAGS to compile with covafill
cxxFlags()
cxxFlags()
Returns a string with the CXXFLAGS needed to compile C++ code using covafill.
Christoffer Moesgaard Albertsen
## Not run: if(require("TMB")){ f <- system.file("examples","tmbtest","tmbtest.cpp", package='covafillr') TMB::compile(f,CXXFLAGS = cxxFlags()) } ## End(Not run)
## Not run: if(require("TMB")){ f <- system.file("examples","tmbtest","tmbtest.cpp", package='covafillr') TMB::compile(f,CXXFLAGS = cxxFlags()) } ## End(Not run)
Wrapper for the covafill reference class to do kernel density estimation.
kde(X, bw = suggestBandwith(X, -1), npred = 100, from = min(X), to = max(X))
kde(X, bw = suggestBandwith(X, -1), npred = 100, from = min(X), to = max(X))
X |
A numeric matrix or vector of data coordinates |
bw |
Bandwith used |
npred |
Number of coordinate wise equally spaced points at which the density is to be estimated. The numbers are repeated if the length is less than the dimension of the coordinates. |
from |
Coordinate wise lower bound of points at which the density is to be estimated. The numbers are repeated if the length is less than the dimension of the coordinates. |
to |
Coordinate wise upper bound of points at which the density is to be estimated. The numbers are repeated if the length is less than the dimension of the coordinates. |
a list of coordinates and corresponding density estimates
Christoffer Moesgaard Albertsen
Calls rjags::load.module with appropriate arguments to load the covafillr module.
loadJAGSModule()
loadJAGSModule()
Nothing
Christoffer Moesgaard Albertsen
if(require("rjags") & covafillr:::.installed_with_jags) loadJAGSModule()
if(require("rjags") & covafillr:::.installed_with_jags) loadJAGSModule()
As an extention to the ggplot2
package, the function adds a covafill fit to an (x,y) plot. The fit is predicted to points on the interval range(x).
stat_covafill( mapping = NULL, data = NULL, geom = "smooth", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, n = 50, bandwith = NULL, polyDegree = 3L, level = 0.95, se = TRUE, ... )
stat_covafill( mapping = NULL, data = NULL, geom = "smooth", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, n = 50, bandwith = NULL, polyDegree = 3L, level = 0.95, se = TRUE, ... )
mapping |
Set of mappings created by 'aes' from the |
data |
The data to be displayed in this layer. The same as |
geom |
The same as |
position |
Position adjustments. The same as |
na.rm |
Not used |
show.legend |
Should this legend be displayed? The same as |
inherit.aes |
The same as |
n |
Number of points to do prediction on. |
bandwith |
Bandwith used in covafill. Uses |
polyDegree |
Polynomial degree to use in covafill. |
level |
Level of confidence interval to use. |
se |
Should confidence intervals be displayed? |
... |
Other arguments passed to |
A ggplot2
layer
.
Christoffer Moesgaard Albertsen
The bandwith is suggested coordinate wise to be
where p is the polynomial degree used and n is the number of coordinate points.
suggestBandwith(X, p)
suggestBandwith(X, p)
X |
A numeric matrix or vector of data coordinates |
p |
Polynomial degree to base the suggestion on |
a vector or scalar of suggested bandwiths
Christoffer Moesgaard Albertsen