
Semiparametric regression: worked examples
Dongwen Luo
Source:vignettes/articles/semireg-examples.Rmd
semireg-examples.RmdCode
library(tidyverse)
library(predictmeans)
library(HRW)
library(gridExtra)
library(car)
library(DT)
library(gt)
library(knitr)
data(copper, package = "predictmeans")
# an HTML table with a CSV download button (choose 'All' entries to download every row)
DT_df <- function(df){
stopifnot(is.data.frame(df))
df |>
DT::datatable(
extensions = 'Buttons',
options = list(
dom = 'Blfrtip', # set DOM
buttons = c('csv'),
lengthMenu = list(
c(10, 20, -1), # declare values
c(10, 20, "All") # declare titles
), # end of lengthMenu customization
pageLength = 10
), rownames= FALSE)
}These five examples fit semiparametric models with
semireg() and use predictmeans for the tests, predictions
and contrasts. The general workflow (data and model, curves with bands,
where curves differ, derivatives) is in
vignette("smooth", package = "predictmeans"); this page
shows it on real data with groups, several covariates, binary and count
responses, and a spatial surface. They were Examples 4 to 8 of the overview.
Example 1: semiparametric regression with groups (WarsawApts data)
WarsawApts (package HRW) is a subset of the data set
apartments in the package PBImisc: the prices of apartments
sold in Warsaw, Poland, from 2007 to 2009, with their construction dates
and districts.
Data Info
Code
## 'data.frame': 409 obs. of 6 variables:
## $ surface : num 20 27 28 28 30 32 32 36 36 37 ...
## $ district : Factor w/ 4 levels "Mokotow","Srodmiescie",..: 2 3 1 1 2 1 2 3 3 1 ...
## $ n.rooms : int 1 1 1 2 1 2 1 2 2 2 ...
## $ floor : int 7 1 1 4 1 3 7 6 5 7 ...
## $ construction.date: int 1970 1962 1950 1968 1952 2007 1961 1965 1968 1972 ...
## $ apartment.price : num 95.2 117.4 114.3 114.3 93.8 ...
Code
ggplot(WarsawApts, aes(x=construction.date, y=apartment.price, color=district))+
geom_point(size=1.6)+
theme_bw(15)
ANOVA, Smooth Tests and
- ANOVA
| F | Df | Df.res | Pr(>F) | |
|---|---|---|---|---|
| (Intercept) | 0.0427881 | 1 | 149.228 | 0.8364068 |
| construction.date | 1.4569482 | 1 | 149.742 | 0.2293196 |
- Tests of the smooth terms
| term | variance | logLik | logLik.without | LRT | p.value | test |
|---|---|---|---|---|---|---|
| s_construction_date | 2098.633 | -1772.452 | -1844.882 | 144.859 | 1e-04 | exact restricted LRT (10000 simulations) |
Code
R2_model(mod1$semer)## # Adjusted R2 for mixed models (Piepho)
## Total Fixed Random s_construction_date
## 86.14% 0.03% 86.11% 86.11%
Smooth with Group
Code
# a curve per district, each with its own smoothing variance; district and the
# district slopes (district + district:construction.date) are added automatically
mod2 <- semireg(apartment.price ~ s(construction.date, by = district, bs = "os", k = 10),
data = WarsawApts)Code
residplot(mod2$semer, group="district") 
ANOVA, Smooth Tests and
- ANOVA
| F | Df | Df.res | Pr(>F) | |
|---|---|---|---|---|
| (Intercept) | 3.387879 | 1 | 15.39330 | 0.0850357 |
| district | 1.548713 | 3 | 19.13416 | 0.2343467 |
| district:construction.date | 1.219511 | 4 | 26.48372 | 0.3261633 |
- Tests of the smooth terms
| term | variance | logLik | logLik.without | LRT | p.value | test |
|---|---|---|---|---|---|---|
| s_construction_date_districtMokotow | 893.642404 | -1751.525 | -1800.940 | 98.829583 | 0.0000000 | 0.5 chi2(0) + 0.5 chi2(1) |
| s_construction_date_districtSrodmiescie | 1612.815730 | -1751.525 | -1761.894 | 20.737475 | 0.0000026 | 0.5 chi2(0) + 0.5 chi2(1) |
| s_construction_date_districtWola | 8.239334 | -1751.525 | -1752.495 | 1.939841 | 0.0818429 | 0.5 chi2(0) + 0.5 chi2(1) |
| s_construction_date_districtZoliborz | 51.288854 | -1751.525 | -1760.028 | 17.004627 | 0.0000186 | 0.5 chi2(0) + 0.5 chi2(1) |
| s(construction.date):district (4 blocks jointly) | NA | -1751.525 | -1812.510 | 121.968606 | 0.0000000 | binomial mixture of chi2(0..4) |
Code
R2_model(mod2$semer)## # Adjusted R2 for mixed models (Piepho)
## Total Fixed
## 89.98% 0.69%
## Random s_construction_date_districtMokotow
## 89.29% 31.79%
## s_construction_date_districtSrodmiescie s_construction_date_districtWola
## 55.42% 0.27%
## s_construction_date_districtZoliborz
## 1.81%
Example 2: semiparametric regression with several covariates (Caschool data)
The Caschool data are a cross-section of 420 Californian
school districts in 1998-1999: county, grade span of the district,
percentage of pupils qualifying for CalWORKs (income assistance),
computers per student, average district income and average mathematics
score.
Data Info
Code
## 'data.frame': 420 obs. of 7 variables:
## $ county : Factor w/ 45 levels "Alameda","Butte",..: 1 2 2 2 2 6 29 11 6 25 ...
## $ grspan : Factor w/ 2 levels "KK-06","KK-08": 2 2 2 2 2 2 2 2 2 1 ...
## $ calwpct : num 0.51 15.42 55.03 36.48 33.11 ...
## $ compstu : num 0.344 0.421 0.109 0.35 0.128 ...
## $ avginc : num 22.69 9.82 8.98 8.98 9.08 ...
## $ mathscr : num 690 662 651 644 640 ...
## $ log.avginc: num 3.12 2.28 2.19 2.19 2.21 ...
Code
summary(Caschool)## county grspan calwpct compstu
## Sonoma : 29 KK-06: 61 Min. : 0.000 Min. :0.00000
## Kern : 27 KK-08:359 1st Qu.: 4.395 1st Qu.:0.09377
## Los Angeles: 27 Median :10.520 Median :0.12546
## Tulare : 24 Mean :13.246 Mean :0.13593
## San Diego : 21 3rd Qu.:18.981 3rd Qu.:0.16447
## Santa Clara: 20 Max. :78.994 Max. :0.42083
## (Other) :272
## avginc mathscr log.avginc
## Min. : 5.335 Min. :605.4 Min. :1.674
## 1st Qu.:10.639 1st Qu.:639.4 1st Qu.:2.365
## Median :13.728 Median :652.4 Median :2.619
## Mean :15.317 Mean :653.3 Mean :2.645
## 3rd Qu.:17.629 3rd Qu.:665.8 3rd Qu.:2.870
## Max. :55.328 Max. :709.5 Max. :4.013
##
Smooth Modelling
Code
mod <- semireg_tmb(mathscr ~ s(calwpct, bs = "os", k = 12) + s(log.avginc, bs = "os", k = 5) +
s(compstu, bs = "os", k = 12) + (1 | county) + (1 | grspan),
data = Caschool)Code
residplot(mod$semer) 
ANOVA and
- ANOVA
| Chisq | Df | Pr(>Chisq) | |
|---|---|---|---|
| calwpct | 35.005795 | 1 | 0.0000000 |
| log.avginc | 90.020820 | 1 | 0.0000000 |
| compstu | 1.896853 | 1 | 0.1684309 |
Code
R2_model(mod$semer)## # Adjusted R2 for mixed models (Piepho)
## Total Fixed Random county grspan s_calwpct
## 72.60% 50.87% 21.73% 8.90% 0.03% 9.12%
## s_log_avginc s_compstu
## 2.97% 0.72%
Example 3: semiparametric regression with a binary response (indonRespir data)
The Indonesian Children’s Health Study followed a cohort of 275 children for respiratory infections. The data are longitudinal: each child was examined between one and six times. The curves below are the probability of infection for a typical child (random effect zero); the pink ticks mark the ages of the children without (bottom) and with (top) an infection.
Data Info
Code
## 'data.frame': 1200 obs. of 13 variables:
## $ idnum : Factor w/ 275 levels "1","2","3","4",..: 1 1 1 1 1 1 2 2 2 2 ...
## $ respirInfec: int 0 0 0 0 1 0 0 0 0 0 ...
## $ age : num 5.58 5.83 6.08 6.33 6.58 ...
## $ vitAdefic : int 0 0 0 0 0 0 0 0 0 0 ...
## $ female : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 2 2 2 2 ...
## $ height : int -3 -3 -2 -2 -2 -3 2 0 -1 -2 ...
## $ stunted : int 0 0 0 0 0 0 0 0 0 0 ...
## $ visit2 : int 0 1 0 0 0 0 0 1 0 0 ...
## $ visit3 : int 0 0 1 0 0 0 0 0 1 0 ...
## $ visit4 : int 0 0 0 1 0 0 0 0 0 1 ...
## $ visit5 : int 0 0 0 0 1 0 0 0 0 0 ...
## $ visit6 : int 0 0 0 0 0 1 0 0 0 0 ...
## $ sex : Factor w/ 2 levels "male","female": 1 1 1 1 1 1 2 2 2 2 ...
Code
summary(indonRespir)## idnum respirInfec age vitAdefic female
## 1 : 6 Min. :0.00000 Min. :0.3333 Min. :0.00000 0:708
## 2 : 6 1st Qu.:0.00000 1st Qu.:1.7500 1st Qu.:0.00000 1:492
## 3 : 6 Median :0.00000 Median :3.2500 Median :0.00000
## 5 : 6 Mean :0.08917 Mean :3.2669 Mean :0.04583
## 10 : 6 3rd Qu.:0.00000 3rd Qu.:4.5833 3rd Qu.:0.00000
## 11 : 6 Max. :1.00000 Max. :7.1667 Max. :1.00000
## (Other):1164
## height stunted visit2 visit3
## Min. :-23.0000 Min. :0.0000 Min. :0.0000 Min. :0.0000
## 1st Qu.: -3.0000 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.0000
## Median : 1.0000 Median :0.0000 Median :0.0000 Median :0.0000
## Mean : 0.9058 Mean :0.1233 Mean :0.1783 Mean :0.1475
## 3rd Qu.: 4.0000 3rd Qu.:0.0000 3rd Qu.:0.0000 3rd Qu.:0.0000
## Max. : 25.0000 Max. :1.0000 Max. :1.0000 Max. :1.0000
##
## visit4 visit5 visit6 sex
## Min. :0.0000 Min. :0.0000 Min. :0.0000 male :708
## 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.0000 female:492
## Median :0.0000 Median :0.0000 Median :0.0000
## Mean :0.1525 Mean :0.1625 Mean :0.1675
## 3rd Qu.:0.0000 3rd Qu.:0.0000 3rd Qu.:0.0000
## Max. :1.0000 Max. :1.0000 Max. :1.0000
##
Smooth without Group
Code
mod1 <- semireg_tmb(respirInfec ~ s(age, bs = "os", k = 25) + vitAdefic + sex + height + stunted +
visit2 + visit3 + visit4 + visit5 + visit6 + (1 | idnum),
family = binomial, data = indonRespir)Code
residplot(mod1$semer) 
ANOVA and
- ANOVA
| Chisq | Df | Pr(>Chisq) | |
|---|---|---|---|
| vitAdefic | 1.7004525 | 1 | 0.1922288 |
| sex | 3.6344237 | 1 | 0.0565962 |
| height | 1.4758603 | 1 | 0.2244233 |
| stunted | 0.9708859 | 1 | 0.3244593 |
| visit2 | 7.8007540 | 1 | 0.0052224 |
| visit3 | 2.2953732 | 1 | 0.1297600 |
| visit4 | 7.6881957 | 1 | 0.0055583 |
| visit5 | 2.0926505 | 1 | 0.1480091 |
| visit6 | 0.0000633 | 1 | 0.9936497 |
| age | 15.5045938 | 1 | 0.0000823 |
Code
R2_model(mod1$semer)## # Adjusted R2 for mixed models (Piepho)
## Total Fixed Random idnum s_age
## 33.80% 17.33% 16.47% 13.99% 2.48%
##
## Advice:
## i For a non-gaussian model the R2 is on the link (latent) scale: not
## comparable with an lm R2 or across families and links; use it to compare
## models of the same family.
Smooth with Group
Code
# a curve of age for each sex (sex and sex:age are added to the fixed effects)
mod2 <- semireg_tmb(respirInfec ~ s(age, by = sex, bs = "os", k = 25) + vitAdefic + height + stunted +
visit2 + visit3 + visit4 + visit5 + visit6 + (1 | idnum),
family = binomial, data = indonRespir)Code
residplot(mod2$semer) 
ANOVA and
- ANOVA
| Chisq | Df | Pr(>Chisq) | |
|---|---|---|---|
| vitAdefic | 1.5282678 | 1 | 0.2163726 |
| height | 1.6518437 | 1 | 0.1987082 |
| stunted | 0.9757535 | 1 | 0.3232495 |
| visit2 | 7.5616474 | 1 | 0.0059623 |
| visit3 | 1.9898132 | 1 | 0.1583604 |
| visit4 | 7.0981395 | 1 | 0.0077164 |
| visit5 | 2.7020790 | 1 | 0.1002175 |
| visit6 | 0.0332517 | 1 | 0.8553077 |
| sex | 0.8920972 | 1 | 0.3449096 |
| sex:age | 20.0324865 | 2 | 0.0000447 |
Code
R2_model(mod2$semer)## # Adjusted R2 for mixed models (Piepho)
## Total Fixed Random idnum s_age_sexmale
## 33.67% 16.27% 17.40% 14.70% 2.70%
## s_age_sexfemale
## 0.00%
##
## Advice:
## i For a non-gaussian model the R2 is on the link (latent) scale: not
## comparable with an lm R2 or across families and links; use it to compare
## models of the same family.
Prediction with Group
Code

The difference in the probability of infection between boys and girls along age, with its 95% confidence band:
Code
autoplot(pred_out2_2)
Example 4: semiparametric regression with a negative binomial response (ragweed data)
The ragweed data (package HRW) give daily ragweed pollen
counts and weather measurements for 334 days in Kalamazoo, Michigan,
USA, over four years. The counts are overdispersed, so a negative
binomial model is used.
Data Info
## 'data.frame': 334 obs. of 7 variables:
## $ pollenCount : int 7 7 2 5 4 0 0 0 13 62 ...
## $ year : Factor w/ 4 levels "1991","1992",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ dayInSeason : int 1 2 3 4 5 6 7 8 9 10 ...
## $ temperature : num 72.4 67.1 68.5 73.4 80.5 ...
## $ temperatureResidual: num 0.649 -4.796 -3.521 1.207 8.14 ...
## $ rain : int 1 0 1 1 1 1 1 1 1 1 ...
## $ windSpeed : num 10.4 9.4 8.2 11.8 7.2 7.8 5.4 10.2 5.3 8.8 ...
Code
summary(ragweed)## pollenCount year dayInSeason temperature temperatureResidual
## Min. : 0.00 1991:92 Min. : 1.00 Min. :37.31 Min. :-13.98210
## 1st Qu.: 1.00 1992:81 1st Qu.:22.00 1st Qu.:55.47 1st Qu.: -4.31318
## Median : 9.00 1993:87 Median :43.50 Median :64.66 Median : -0.05665
## Mean : 44.45 1994:74 Mean :43.29 Mean :63.29 Mean : 0.01246
## 3rd Qu.: 56.00 3rd Qu.:64.00 3rd Qu.:71.89 3rd Qu.: 4.16160
## Max. :440.00 Max. :92.00 Max. :84.33 Max. : 16.43900
## rain windSpeed
## Min. :0.0000 Min. : 0.000
## 1st Qu.:1.0000 1st Qu.: 6.850
## Median :1.0000 Median : 9.200
## Mean :0.8982 Mean : 8.865
## 3rd Qu.:1.0000 3rd Qu.:10.800
## Max. :1.0000 Max. :17.800
Smooth Modelling
Code
# a seasonal curve for each year (with year and year:dayInSeason as fixed terms),
# and curves in temperature and wind speed
mod <- semireg_tmb(pollenCount ~ rain + s(dayInSeason, by = year, bs = "os") +
s(temperatureResidual, bs = "os") + s(windSpeed, bs = "os"),
family = nbinom1, data = ragweed)Code
residplot(mod$semer) 
ANOVA and
- ANOVA
| Chisq | Df | Pr(>Chisq) | |
|---|---|---|---|
| rain | 29.05127 | 1 | 1.0e-07 |
| year | 25.70815 | 3 | 1.1e-05 |
| temperatureResidual | 72.10172 | 1 | 0.0e+00 |
| windSpeed | 76.13481 | 1 | 0.0e+00 |
| year:dayInSeason | 45.61102 | 4 | 0.0e+00 |
Code
R2_model(mod$semer)## # Adjusted R2 for mixed models (Piepho)
## Total Fixed Random
## 60.26% 16.37% 43.89%
## s_dayInSeason_year1991 s_dayInSeason_year1992 s_dayInSeason_year1993
## 13.61% 11.43% 12.32%
## s_dayInSeason_year1994 s_temperatureResidual s_windSpeed
## 6.53% 0.00% 0.00%
##
## Advice:
## i For a non-gaussian model the R2 is on the link (latent) scale: not
## comparable with an lm R2 or across families and links; use it to compare
## models of the same family.
Example 5: semiparametric regression with a spatial surface (copper data)
The copper data give the copper grade of ore samples
with the X, Y and Z coordinates of each sampling point in a mining area.
The model has a curve in depth and a surface over the plane, on the log
scale; the predictions are back-transformed with
trans = exp (medians on the grade scale).
Data Info
Code
str(copper)## 'data.frame': 439 obs. of 4 variables:
## $ xcoord: int 220 220 220 220 220 220 220 220 220 270 ...
## $ ycoord: int 625 625 625 625 625 625 625 625 625 625 ...
## $ zcoord: int 122 117 112 107 102 97 92 87 82 122 ...
## $ grade : num 1.34 0.98 1.29 1.18 1.1 0.46 1.18 0.96 1.27 0.64 ...
Code
summary(copper)## xcoord ycoord zcoord grade
## Min. : 65.0 Min. :220.0 Min. : 77.0 Min. :0.370
## 1st Qu.:365.0 1st Qu.:320.0 1st Qu.: 87.0 1st Qu.:0.740
## Median :465.0 Median :425.0 Median :102.0 Median :0.970
## Mean :469.5 Mean :427.8 Mean :100.4 Mean :1.058
## 3rd Qu.:610.0 3rd Qu.:525.0 3rd Qu.:112.0 3rd Qu.:1.245
## Max. :665.0 Max. :625.0 Max. :122.0 Max. :3.200
Code
DT_df(copper)Modelling
Code
# an O'Sullivan spline in depth, a thin-plate surface over the plane (their linear
# terms are added to the fixed effects), and the pairwise interactions of the coordinates
mod <- semireg(log(grade) ~ s(zcoord, bs = "os") + s(xcoord, ycoord, bs = "ztps", k = 6) +
xcoord:ycoord + xcoord:zcoord + ycoord:zcoord,
data = copper)Code
residplot(mod$semer) 
ANOVA, Smooth Tests and
- ANOVA
| F | Df | Df.res | Pr(>F) | |
|---|---|---|---|---|
| (Intercept) | 0.8534314 | 1 | 14.25096 | 0.3709616 |
| zcoord | 26.5094685 | 1 | 417.28577 | 0.0000004 |
| xcoord | 0.0247375 | 1 | 13.95960 | 0.8772752 |
| ycoord | 1.4111370 | 1 | 14.17006 | 0.2543982 |
| xcoord:ycoord | 0.2820408 | 1 | 15.46128 | 0.6029130 |
| xcoord:zcoord | 3.9173411 | 1 | 416.02554 | 0.0484493 |
| ycoord:zcoord | 25.9679209 | 1 | 413.16877 | 0.0000005 |
- Tests of the smooth terms
| term | variance | logLik | logLik.without | LRT | p.value | test |
|---|---|---|---|---|---|---|
| s_zcoord | 0.0789849 | -159.2031 | -199.6525 | 80.89885 | 0 | 0.5 chi2(0) + 0.5 chi2(1) |
| s_xcoord_ycoord | 1.7857080 | -159.2031 | -207.6783 | 96.95054 | 0 | 0.5 chi2(0) + 0.5 chi2(1) |
Code
R2_model(mod$semer)## # Adjusted R2 for mixed models (Piepho)
## Total Fixed Random s_xcoord_ycoord s_zcoord
## 79.20% -135.63% 214.83% 196.83% 18.00%
References
Benjamini, Y., and Hochberg, Y. (1995). Controlling the false discovery rate: a practical and powerful approach to multiple testing. Journal of the Royal Statistical Society Series B, 57, 289–300. https://doi.org/10.1111/j.2517-6161.1995.tb02031.x. https://www.jstor.org/stable/2346101.
Cave, V. (2021). Confidence tricks: the 83.4% confidence interval for comparing means. https://vsni.co.uk/blogs/confidence_trick.
Douglas Bates, Martin Maechler, Ben Bolker, Steve Walker (2015). Fitting Linear Mixed-Effects Models Using lme4. Journal of Statistical Software, 67(1), 1-48. https://doi.org/10.18637/jss.v067.i01.
Harezlak J., Ruppert, D. & Wand, M.P. (2018). Semiparametric Regression with R. Springer; ISBN: 978-1-4939-8851-8.
Kenward, M. G., and Roger, J. H. (1997). Small sample inference for fixed effects from restricted maximum likelihood. Biometrics, 53, 983-997. https://doi.org/10.2307/2533558.
Luo, D., Ganesh, S., and Koolaard, J. predictmeans: predicted means for linear and semiparametric models. R package. https://CRAN.R-project.org/package=predictmeans.
Piepho, H.-P. (2023). An adjusted coefficient of determination (R2) for generalized linear mixed models in one go. Biometrical Journal, 65(7), e2200290. https://doi.org/10.1002/bimj.202200290.
R Core Team (2025). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. URL https://www.R-project.org/.












