Bug 16540 - groupedData within a function does not work
Summary: groupedData within a function does not work
Status: CLOSED Works as documented
Alias: None
Product: R
Classification: Unclassified
Component: I/O (show other bugs)
Version: R 3.2.1
Hardware: x86_64/x64/amd64 (64-bit) OS X Yosemite
: P5 major
Assignee: R-core
Depends on:
Reported: 2015-09-21 20:30 UTC by Brandon Coombes
Modified: 2018-08-25 06:48 UTC (History)
2 users (show)

See Also:

one possible way to implement the feature (1.00 KB, patch)
2018-08-25 06:48 UTC, Benjamin Tyner
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Brandon Coombes 2015-09-21 20:30:28 UTC
Below will produce the issue with groupedData. Xs will not be found by groupedData. I cannot define the groupedData object outside of the function because my function is doing a model selection on the X and making it one vector (Xs). 


famid <- rep(1:(500+500),each=2)
mz <- c(rep(1,2*500),rep(0,2*500))
Y <- rnorm(2000)
X <- matrix(rnorm(20000),nrow=2000)
Xg <- matrix(rnorm(20000),nrow=2000)

tester <- function(Y,X,Xg,mz,famid){
	n <- length(Y)
	dummyId <- factor(rep(1,n))
	## set up random effects
	Xs <- X%*%as.matrix(rbinom(10,1,0.5))
	dat <- groupedData(Y~1+Xs|dummyId)
	Z.block <- pdBlocked(list(dummyId=pdIdent(~0+Xg),famid=pdIdent(~0+mz)))	
	## run model and obtain variance terms
	fit <- lme(Y~1+X,random=Z.block,data=dat,method="ML")
Comment 1 Benjamin Tyner 2018-08-19 00:30:09 UTC
I don't think this is a bug; groupedData() needs you to supply a 'data' argument:

   data <- data.frame(Y, Xs, dummyId)
   dat <- groupedData(Y~1+Xs|dummyId, data = data)
Comment 2 Martin Maechler 2018-08-24 07:09:59 UTC
Thank you, Benjamin, you are right on spot.

In general, thank you very much for your valuable and 
highly appreciated bugs-r-project.org monitoring and cleanup work !!
Comment 3 Benjamin Tyner 2018-08-25 06:48:11 UTC
Created attachment 2361 [details]
one possible way to implement the feature

On the other hand, we might interpret this as a feature request, in which case here is a patch that *might* be suitable.