Error running analysis after deleting rows from data frame

Posted on
Picture of user. dstanley4 Joined: 08/07/2012
Forums

Hi Everyone,
Using metaSEM and loving it. But, I've encountered a problem using metaSEM with a data frame after I have deleted rows from that data frame -- or created a new data frame that is a subset of the rows in the original data frame. Fortunately, I've also discovered a work around - but the error is an odd one. I have attached the relevant files, but I'll describe it in detail here.

Imagine I have a data set then runs fine in metaSEM. If I create new data frame that is a subset of the original data frame the new data frame will not run in metaSEM. I've tried various ways of creating the new data frame, but the results is always the same. I get the following error:

Error in `$<-.data.frame`(`*tmp*`, "time", value = c(1L, 0L, 1L, 2L, 3L, :
replacement has 19 rows, data has 17

Oddly, I've found that if I take the data frame that will not run, save it to disk, load it, and run it, everything is fine.
Looks like a fix may possibly be needed in metaSEM or OpenMx - since the particular data doesn't seem to be the issue here.
Cheers,
David

library(OpenMx)
library(metaSEM)
testData=read.csv("ExampleData2.csv")
testDataSubset=testData[testData$GRP!="A",]

print("Running: All Data")
m1=meta3(y=r,v=va,cluster=GRP,data=testData)

#if you uncomment this next bit, the program will crash with the above error
#print("Running Subset Data: Error")
#m3=meta3(y=r,v=va,cluster=GRP,data=testDataSubset)

#This work around save the day though...
write.table(testDataSubset,"testDataSubsetTemp",row.names=T)
testDataSubsetLoad=read.table("testDataSubsetTemp")
print("Running Subset Data: saved and reloaded")
m2=meta3(y=r,v=va,cluster=GRP,data=testDataSubsetLoad)

Replied on Fri, 11/09/2012 - 21:50
Picture of user. Mike Cheung Joined: 10/08/2009

The levels of GRP are different in these two datasets:
> levels(testDataSubset$GRP)
[1] "A" "B" "C" "D" "E" "F"
> levels(testDataSubsetLoad$GRP)
[1] "B" "C" "D" "E" "F"

A workaround is to use as.character(GRP):
m3 <- meta3(y=r,v=va,cluster=as.character(GRP),data=testDataSubset)

I will include as.character() in the next release. Thanks.

Mike

Replied on Tue, 11/27/2012 - 15:46
Picture of user. dstanley4 Joined: 08/07/2012

In reply to by Mike Cheung

Thanks for this solution. I tried it immediately - and it solved my problems. Sorry to take a while to get back to this forum to thank you - busy time of year.
Cheers,
David