library(JOPS)
library(colorout)
library(ggplot2)
# Data
xl = c(-3.5, -2.5, -1.5, -0.5, 0.5, 1.5)
xr = c(-2.51, -1.51, -0.51, 0.49, 1.49, 30)
y = c(9, 32, 109, 180, 132, 38)
# Set-ups
m = length(y)
n = 500
wdts = xr - xl
dens = y/wdts/sum(y)
# Composition matrix C 1 if finer bin is included in the coarse one, 0 otherwise
x = seq(min(xl)-3, max(xr) + 3, len = n)
C = matrix(0, m, n)
for(i in 1:m) C[i, x >= xl[i] & x <= xr[i]] = 1
# Prepare B-splines bases
B = JOPS::bbase(x, nseg = 30)
# Fit
mod = JOPS::pclm(y, C, B, lambda = 5, pord = 2, show = T)
est = mod$gamma / diff(x)[1] / sum(mod$gamma)
# Plot
fit = data.frame(x = x, fits = est)
dat = data.frame(xl = xl, xr = xr, y = dens)
plt = ggplot(dat, aes(ymin = 0)) +
geom_rect(aes(xmin = xl, xmax = xr, ymax = y, fill = I('white'), color = I('black'))) +
geom_line(aes(x = x, y = fits), data = fit, col = I('red')) +
ylab('Density') +
theme_bw()
plt
library(JOPS)
library(colorout)
library(ggplot2)
# Data
xl = c(-3.5, -2.5, -1.5, -0.5, 0.5, 1.5)
xr = c(-2.51, -1.51, -0.51, 0.49, 1.49, 30)
y = c(9, 32, 109, 180, 132, 38)
# Set-ups
m = length(y)
n = 500
wdts = xr - xl
dens = y/wdts/sum(y)
# Composition matrix C 1 if finer bin is included in the coarse one, 0 otherwise
x = seq(min(xl)-3, max(xr) + 3, len = n)
C = matrix(0, m, n)
for(i in 1:m) C[i, x >= xl[i] & x <= xr[i]] = 1
# Prepare B-splines bases
B = JOPS::bbase(x, nseg = 30)
# Fit
mod = JOPS::pclm(y, C, B, lambda = 5, pord = 2, show = T)
est = mod$gamma / diff(x)[1] / sum(mod$gamma)
# Plot
fit = data.frame(x = x, fits = est)
dat = data.frame(xl = xl, xr = xr, y = dens)
plt = ggplot(dat, aes(ymin = 0)) +
geom_rect(aes(xmin = xl, xmax = xr, ymax = y, fill = I('white'), color = I('black'))) +
geom_line(aes(x = x, y = fits), data = fit, col = I('red')) +
ylab('Density') +
theme_bw()
plt