Skip to content

Commit bdc0888

Browse files
author
roberto maestre
committed
Initial pull
1 parent 0278e2a commit bdc0888

File tree

86 files changed

+1134
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+1134
-0
lines changed

DESCRIPTION

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Package: additiveRegressionSpline
2+
Type: Package
3+
Title: C++ implementation of regression splines with academic purpose
4+
Version: 0.1.0
5+
Author: Roberto Maestre
6+
Maintainer: Roberto Maestre <roberto.maestre@bbvadata.com>
7+
Description: C++ implementation of examples from "Generalized Additive Models: An Introduction with R, Simon N. Wood".
8+
License: GPL-2
9+
LazyData: TRUE
10+
Imports: Rcpp (>= 0.11.0)
11+
LinkingTo: Rcpp,RcppArmadillo
12+
Depends:
13+
R (>= 3.0.3),
14+
data.table,
15+
ggplot2,
16+
mgcv

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
useDynLib(additiveRegressionSpline)
2+
exportPattern("^[[:alpha:]]+")
3+
importFrom(Rcpp, evalCpp)

R/RcppExports.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
2+
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
3+
4+
hatMatrix <- function(m, length) {
5+
.Call('additiveRegressionSpline_hatMatrix', PACKAGE = 'additiveRegressionSpline', m, length)
6+
}
7+
8+
cubicSpline <- function(x, knots) {
9+
.Call('additiveRegressionSpline_cubicSpline', PACKAGE = 'additiveRegressionSpline', x, knots)
10+
}
11+
12+
splineModel <- function(x, knots, y) {
13+
.Call('additiveRegressionSpline_splineModel', PACKAGE = 'additiveRegressionSpline', x, knots, y)
14+
}
15+
16+
splineModelPenalized <- function(y, x, knots, lambda) {
17+
.Call('additiveRegressionSpline_splineModelPenalized', PACKAGE = 'additiveRegressionSpline', y, x, knots, lambda)
18+
}
19+
20+
splineModelPMatrix <- function(x, z, knots_x, knots_z) {
21+
.Call('additiveRegressionSpline_splineModelPMatrix', PACKAGE = 'additiveRegressionSpline', x, z, knots_x, knots_z)
22+
}
23+
24+
splineModelPMatrixFit <- function(y, x, modelMatrix, sX, sZ, lambda_x, lambda_z) {
25+
.Call('additiveRegressionSpline_splineModelPMatrixFit', PACKAGE = 'additiveRegressionSpline', y, x, modelMatrix, sX, sZ, lambda_x, lambda_z)
26+
}
27+

R/gam.R

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
rk <- function(x,z) {
2+
((z-0.5)^2 - 1/12)*((x-0.5)^2 - 1/12)/4 -
3+
((abs(x-z)-0.5)^4-(abs(x-z)-0.5)^2/2 + 7/240) / 24
4+
}
5+
6+
spl.X <- function(x,knots){
7+
q <- length(knots) + 2 # number of parameters
8+
n <- length(x) # number of observations
9+
X <- matrix(1, n, q) # initialized model matrix
10+
X[,2] <- x # set second column to x
11+
X[,3:q] <- outer(x, knots, FUN = rk) # remaining to cubic spline
12+
X
13+
}
14+
prs.fit <- function(y, x, knots, lambda) {
15+
q = length(knots) + 2 # dimension of basis
16+
n = length(x) # number of observations
17+
Xa = rbind(spl.X(x, knots), mat.sqrt(spl.S(knots))*sqrt(lambda)) # augmented model matrix
18+
y[(n+1):(n+q)] = 0 # augment the data vector
19+
lm(y ~ Xa - 1) # fit and return penalized regression spline
20+
}
21+
spl.S <- function(knots) {
22+
q = length(knots) + 2
23+
S = matrix(0, q, q) # initialize matrix
24+
S[3:q, 3:q] = outer(knots, knots, FUN=rk) # fill in non-zero part
25+
S
26+
}
27+
mat.sqrt <- function(S){
28+
d = eigen(S, symmetric=T)
29+
rS = d$vectors %*% diag(d$values^.5) %*% t(d$vectors)
30+
rS
31+
}
32+
GVC <- function(y,x,knots) {
33+
n <- length(x)
34+
# GCV (R)
35+
mod <- prs.fit(y, x, knots, lambda = 0.0001)
36+
trA <- sum(hatvalues(mod)[1:n])
37+
trA
38+
rss <- sum((y- fitted(mod)[1:n])^2)
39+
rss
40+
41+
(length(x)*rss)/(length(x)-trA)^2
42+
}
43+
trunc <- function(x, ..., prec = 0) base::trunc(x * 10^prec, ...) / 10^prec
44+
45+
am.setup<-function(x,z,q=10)
46+
# Get X, S_1 and S_2 for a simple 2 term AM
47+
{ # choose knots ...
48+
xk <- quantile(unique(x),1:(q-2)/(q-1))
49+
zk <- quantile(unique(z),1:(q-2)/(q-1))
50+
# get penalty matrices ...
51+
S <- list()
52+
S[[1]] <- S[[2]] <- matrix(0,2*q-1,2*q-1)
53+
S[[1]][2:q,2:q] <- spl.S(xk)[-1,-1]
54+
S[[2]][(q+1):(2*q-1),(q+1):(2*q-1)] <- spl.S(zk)[-1,-1]
55+
# get model matrix ...
56+
n<-length(x)
57+
X<-matrix(1,n,2*q-1)
58+
X[,2:q]<-spl.X(x,xk)[,-1] # 1st smooth
59+
X[,(q+1):(2*q-1)]<-spl.X(z,zk)[,-1] # 2nd smooth
60+
list(X=X,S=S)
61+
}
62+
fit.am<-function(y,X,S,sp)
63+
# function to fit simple 2 term additive model
64+
{ # get sqrt of total penalty matrix ...
65+
rS <- mat.sqrt(sp[1]*S[[1]]+sp[2]*S[[2]])
66+
q.tot <- ncol(X) # number of params
67+
n <- nrow(X) # number of data
68+
X1 <- rbind(X,rS) # augmented X
69+
y1 <- c(y,rep(0,q.tot)) # augment data
70+
b<-lm(y1~X1-1) # fit model
71+
trA<-sum(influence(b)$hat[1:n]) # tr(A)
72+
norm<-sum((y-fitted(b)[1:n])^2) # RSS
73+
list(model=b,gcv=norm*n/(n-trA)^2,sp=sp)
74+
}
75+
76+

additiveRegressionSpline.Rproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX
14+
15+
AutoAppendNewline: Yes
16+
StripTrailingWhitespace: Yes
17+
18+
BuildType: Package
19+
PackageUseDevtools: Yes
20+
PackageInstallArgs: --no-multiarch --with-keep.source

0 commit comments

Comments
 (0)