Bug 16540 - groupedData within a function does not work
Summary: groupedData within a function does not work
Status: UNCONFIRMED
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
URL:
Depends on:
Blocks:
 
Reported: 2015-09-21 20:30 UTC by Brandon Coombes
Modified: 2015-09-21 20:30 UTC (History)
0 users

See Also:


Attachments

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). 

library(nlme)

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")
	return(fit)
}