Package 'covafillr'

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

Help Index


A Reference Class for Local Polynomial Regression with covafill.

Description

A Reference Class for Local Polynomial Regression with covafill.

Fields

ptr

External pointer to the covafill C++ object

Methods

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.

Examples

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.

Description

A Reference Class for Search Tree Approximated Local Polynomial Regression with covatree.

Fields

ptr

External pointer to the covatree C++ object

Methods

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.

Examples

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)

CXXFLAGS to compile with covafill

Description

Get CXXFLAGS to compile with covafill

Usage

cxxFlags()

Value

Returns a string with the CXXFLAGS needed to compile C++ code using covafill.

Author(s)

Christoffer Moesgaard Albertsen

See Also

compile

Examples

## Not run: 
if(require("TMB")){
   f <- system.file("examples","tmbtest","tmbtest.cpp", package='covafillr')
   TMB::compile(f,CXXFLAGS = cxxFlags())
}

## End(Not run)

Kernel Density Estimation

Description

Wrapper for the covafill reference class to do kernel density estimation.

Usage

kde(X, bw = suggestBandwith(X, -1), npred = 100, from = min(X), to = max(X))

Arguments

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.

Value

a list of coordinates and corresponding density estimates

Author(s)

Christoffer Moesgaard Albertsen


load JAGS module.

Description

Calls rjags::load.module with appropriate arguments to load the covafillr module.

Usage

loadJAGSModule()

Value

Nothing

Author(s)

Christoffer Moesgaard Albertsen

See Also

load.module

Examples

if(require("rjags") & covafillr:::.installed_with_jags)
   loadJAGSModule()

Add a covafill smoother to an (x,y) plot

Description

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).

Usage

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,
  ...
)

Arguments

mapping

Set of mappings created by 'aes' from the ggplot2 package. The same as ggplot2::stat_smooth.

data

The data to be displayed in this layer. The same as ggplot2::stat_smooth.

geom

The same as ggplot2::stat_smooth.

position

Position adjustments. The same as ggplot2::stat_smooth.

na.rm

Not used

show.legend

Should this legend be displayed? The same as ggplot2::stat_smooth.

inherit.aes

The same as ggplot2::stat_smooth.

n

Number of points to do prediction on.

bandwith

Bandwith used in covafill. Uses suggestBandwith by default.

polyDegree

Polynomial degree to use in covafill.

level

Level of confidence interval to use.

se

Should confidence intervals be displayed?

...

Other arguments passed to layer.

Value

A ggplot2 layer.

Author(s)

Christoffer Moesgaard Albertsen

See Also

stat_smooth


Suggest bandwith for local polynomial regression

Description

The bandwith is suggested coordinate wise to be

0.95min(sd(x),IQR(x)1.349)n1d+4(p+1)0.9 \sqrt{5} \min\left(sd(x),\frac{IQR(x)}{1.349}\right) n ^{-\frac{1}{d+4}} (p+1)

where p is the polynomial degree used and n is the number of coordinate points.

Usage

suggestBandwith(X, p)

Arguments

X

A numeric matrix or vector of data coordinates

p

Polynomial degree to base the suggestion on

Value

a vector or scalar of suggested bandwiths

Author(s)

Christoffer Moesgaard Albertsen