Bug 16806 - nlme getVarCov() fails for corSpatial fits for individuals with a single observation only
Summary: nlme getVarCov() fails for corSpatial fits for individuals with a single obse...
Status: ASSIGNED
Alias: None
Product: R
Classification: Unclassified
Component: Add-ons (show other bugs)
Version: R-devel (trunk)
Hardware: x86_64/x64/amd64 (64-bit) Linux-Ubuntu
: P5 minor
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2016-04-08 12:29 UTC by Sebastian Meyer
Modified: 2016-12-12 14:47 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sebastian Meyer 2016-04-08 12:29:20 UTC
The getVarCov()-methods for "gls" and "lme" fits with spatial correlation structure fail for individuals with a single observation only. This is exemplified in the following code using corExp() correlation:


library("nlme")
data("Phenobarb")
pheno <- subset(Phenobarb, !is.na(conc))

## several subjects only have a single measurement
table(table(getGroups(pheno)))

## gls example
gls1 <- gls(conc ~ time, data = pheno,
            correlation = corExp(form = ~time | Subject))
table(getGroups(gls1))
## Subject "28" has a single observation only
getVarCov(gls1, individual = "28")
## Error in rep(1, nrow(S)) : invalid 'times' argument

## lme example
lme1 <- lme(conc ~ time, data = pheno, random = ~1 | Subject,
            correlation = corExp(form = ~time | Subject))
getVarCov(lme1, type = "conditional", individuals = "28")
## Error in getVarCov.lme(lme1, type = "conditional", individuals = "28") : 
##   length of 'dimnames' [2] not equal to array extent


Both errors result from corMatrix(obj$modelStruct$corStruct) [-> corMatrix.corSpatial] not returning elements for individuals with only one observation:

cormat <- corMatrix(gls1$modelStruct$corStruct)
setdiff(levels(getGroups(gls1)), names(cormat))
## [1] "28" "56" "46" "55" "43" "29" "11" "47" "31"


Note that there are no such errors when using corCAR1 correlation instead of one of the spatial correlation functions:

gls2 <- gls(conc ~ time, data = pheno,
            correlation = corCAR1(form = ~time | Subject))
getVarCov(gls2, individual = "28")
corMatrix(gls2$modelStruct$corStruct)[["28"]]  # matrix(1, 1, 1)

-- 
R Under development (unstable) (2016-03-31 r70407)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.4 LTS

nlme 3.1-126
Comment 1 Martin Maechler 2016-12-12 14:47:53 UTC
The bug is well reproducible.
However this is not a regression, i.e., has probably never worked.

My preliminary code reading suggests need for code changes in C.
I'm happy to be proved wrong... and indeed, this is not of high priority for myself,  so I'd probably need more support before addressing it myself.