# NOT RUN {
#################################################
#################### Example in the manuscript
#################################################
set.seed(1234)
par(mfrow = c(3, 1))
#############################
# Simulating data
#############################
n = 60
t = 1:n
sd = 1
m = n / 2
x = t
y = c(0 * x[t <= n / 3] ,
x[t < 2 * n / 3 & t > n / 3] * 1 ,
0 * x[t >= 2 * n / 3]) + rnorm(n, 0, sd)
# True weights
w = weights = expWeight(
t = t ,
k = 5 ,
l = n/6 ,
m = m ,
plot = 0
)
#############################
# Fitting and ploting data and models
#############################
l = lm(y ~ x, weights = w)
plot(
x ,
y ,
ylim = c(min(y), max(y) * 1.5) ,
col = t %in% seq(n / 3 + 1, 2 * n / 3 - 1) + 1,
cex = 1.5 ,
pch = 16 ,
xlab = 'Time' ,
main = 'Simulated data'
)
abline(v = x[c(n / 3 + 1, 2 * n / 3 - 1)],
lty = 2 ,
lwd = 4 ,
col = 'gray')
abline(l, col = 2 , lty = 2, lwd = 4)
abline(lm(y ~ x) ,
col = 3 ,
lty = 3 ,
lwd = 4)
plot(
t,
w ,
type = 'b' ,
main = 'True weights',
ylab = 'Weight' ,
xlab = 'Time'
)
#############################
# Fitting the Windowing model
#############################
r = SmoothWin(
object = l ,
data = data.frame(y = y, x = x),
t = t ,
m = m ,
min.obs = 4 ,
debug = FALSE
)
#############################
# Plot fitted (windowed) model
#############################
plot(r, main = 'Estimated weights from WGF')
#################################################
#################### Other examples
#################################################
# All examples import the Orthodont dataset from the nlme package
library(nlme)
# Sort the data on the time component (age)
Orthodont = Orthodont[order(Orthodont$age), ]
#############################
# Modes
#############################
mode = which(Orthodont$age %in% c(12))
#############################
# Time component
#############################
time = Orthodont$age
f = formula(distance ~ Sex)
#################################################
#################### Examples ###################
#################################################
### Example 1. Linear model
#############################
# Method 1 (recommanded)
#############################
lm = do.call('lm', list(formula = f, data = Orthodont))
rm(f)
#############################
# Method 2 (can cause error if you pass the formula to the lm function)
# lm = lm(distance ~ Sex, data = Orthodont)
#############################
lm.result = SmoothWin(
object = lm,
data = Orthodont,
t = time,
m = mode,
check = 0,
weightFUN = function(x) {
x
},
debug = TRUE
)
plot(
lm.result,
col = Orthodont$Sex,
pch = as.integer(Orthodont$Sex),
main = 'Simple liner model'
)
#############################
#### Example 2. Linear Model Using Generalized Least Squares
# Method 1 (recommanded)
#############################
f = formula(distance ~ Sex)
gls = do.call('gls', list(model = f, data = Orthodont))
rm(f)
#############################
# Method 2 (can cause error if you pass the formula to the gls function)
# gls = gls(distance ~ Sex, data = Orthodont)
#############################
gls.result = SmoothWin(
object = gls,
data = Orthodont,
t = time,
m = mode,
check = 2,
weightFUN = function(ignore.me) {
varFixed(~ 1 / ModelWeight) #nlme package uses the inverse weights
},
debug = TRUE
)
plot(
gls.result,
col = Orthodont$Sex,
pch = as.integer(Orthodont$Sex),
main = 'Linear model using GLS'
)
#################################################
#### Example 3. Linear mixed model
#################################################
# Method 1 (recommanded)
#############################
fixed = formula(distance ~ Sex)
random = formula(~ 1 | Subject)
lme = do.call('lme', list(
fixed = fixed,
random = random,
data = Orthodont
))
rm(fixed, random)
#############################
# Method 2 (can cause error if you pass the formula to the lme function)
# lme = lme(fixed = distance ~ Sex, random=~1|Subject , data = Orthodont)
#############################
lme.result = SmoothWin(
object = lme,
data = Orthodont,
t = time,
m = mode,
# Remove zero weights as well as single observation dates
check = 1,
weightFUN = function(ignore.me) {
varFixed(~ 1 / ModelWeight)
},
debug = TRUE
)
plot(
lme.result,
col = Orthodont$Sex,
pch = as.integer(Orthodont$Sex),
main = 'Linear mixed model'
)
# }
Run the code above in your browser using DataLab