Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 0, 1

5,217 views
Skip to first unread message

Matt Giovanni

unread,
Jun 27, 2011, 5:24:01 PM6/27/11
to unmarked
Dear unmarked users-

I have been banging my head on this one for too long. I think the
error from the "predict" function has something to do with my use of
the "scale" function in the model statement. Does anyone have any
insights? Thanks and best regards,

Matt


> umf=csvToUMF("ACFL2.csv",long=FALSE,type="unmarkedFramePCount")
> model=pcount(~1 ~scale(PLAND5A)+scale(STREAM)+scale(PLAND5A)*scale(STREAM),umf)
> data=read.csv("ACFL2.csv",header=TRUE)
> range(scale(data$PLAND5A))
[1] -0.7825288 4.5047432
> range(scale(data$STREAM))
[1] -1.270459 4.265686
> DF=data.frame(PLAND5A=seq(-0.7,4),STREAM=-1.2)
> pred=predict(model,type="state",newdata=DF,backTran=FALSE,appendData=FALSE)
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 0, 1
>

Jorge A. Ahumada

unread,
Jun 30, 2011, 10:08:09 PM6/30/11
to Unmarked package, Matt Giovanni
Matt,

I don’t think you need to use scale() when setting up the range values for predict. Setup the dataframe with expected “unscaled” values for your variables.

Jorge

Andy Royle

unread,
Jul 1, 2011, 1:05:48 PM7/1/11
to unma...@googlegroups.com, unmarked
hi Matt,

 "unmarked" users -- I knew I had an email queued up from "matt" and I replied to the wrong one -- so ignore my previous posting which I think was from last fall.

 Anyway, to  Matt Giovanni below -- can you send me your data file and I'll try to reproduce the error this afternoon and see if I can debug it?  

thanks
andy




J. Andy Royle
Research Statistician
USGS Patuxent Wildlife Research Center
12100 Beech Forest Rd.
Laurel, MD 20708
http://profile.usgs.gov/professional/mypage.php?name=aroyle
andy_...@usgs.gov
phone: 301-497-5846
fax: 301-497-5545

Book: "Hierarchical Modeling and Inference in Ecology: The Analysis of Data from Populations, Metapopulations and Communities" by J. A. Royle and R.M. Dorazio.  

unmarked: A very useful R package for fitting certain hierarchical models using likelihood methods. Available from:
http://cran.case.edu/web/packages/unmarked/index.html






From: Matt Giovanni <matthew...@gmail.com>
To: unmarked <unma...@googlegroups.com>
Date: 06/27/2011 05:24 PM
Subject: [unmarked] Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 0, 1
Sent by: unma...@googlegroups.com


Richard Chandler

unread,
Jul 1, 2011, 2:49:28 PM7/1/11
to unma...@googlegroups.com
Hi Matt,

I couldn't reproduce the error, but I know that predict() needs to do a better job handling the model "terms". If Andy hasn't figured out the problem yet, could you try to modify this code to trigger the error? Note that the model formula is equivalent to yours.

library(unmarked)

a <- rnorm(10)
b <- rnorm(10)
y <- matrix(rbinom(30, 1, 0.5), 10, 3)
umf <- unmarkedFramePCount(y=y, siteCovs=data.frame(a=a, b=b))

(fm <- pcount(~1 ~scale(a) * scale(b), umf))
DF <- data.frame(a=-3:3, b=-3:3)
predict(fm, type="state", newdata=DF, backTran=FALSE, appendData=FALSE)


Richard

_____________________________________
Richard Chandler


USGS Patuxent Wildlife Research Center

301-497-5696


-----unma...@googlegroups.com wrote: -----

To: unma...@googlegroups.com
From: Andy Royle <aro...@usgs.gov>
Sent by: unma...@googlegroups.com
Date: 07/01/2011 01:05PM
cc: unmarked <unma...@googlegroups.com>
Subject: Re: [unmarked] Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 0, 1

hi Matt,

thanks
andy


J. Andy Royle

Research Statistician

12100 Beech Forest Rd.

Laurel, MD 20708

http://profile.usgs.gov/professional/mypage.php?name=aroyle

andy_...@usgs.gov

phone: 301-497-5846

fax: 301-497-5545


From:
Matt Giovanni <matthew...@gmail.com>

To:
unmarked <unma...@googlegroups.com>

Sent by:
unma...@googlegroups.com


Richard Chandler

unread,
Jul 1, 2011, 3:01:24 PM7/1/11
to unma...@googlegroups.com
I figured out the problem. If one of the variables in DF is constant, scale() fails. predict() needs to be modified to fix this. Until then, Jorge's suggestion should work.

Richard


_____________________________________
Richard Chandler
USGS Patuxent Wildlife Research Center
301-497-5696


-----unma...@googlegroups.com wrote: -----

To: unma...@googlegroups.com
From: Richard Chandler <rcha...@usgs.gov>
Sent by: unma...@googlegroups.com
Date: 07/01/2011 02:49PM

Matthew Giovanni

unread,
Jul 1, 2011, 6:39:52 PM7/1/11
to unma...@googlegroups.com
Jorge, Andy, Richard-  Problem solved; thanks for your help, as always.  Here's the code I'm using:


umf=csvToUMF("ACFL2.csv",long=FALSE,type="unmarkedFramePCount")
model=pcount(~1 ~scale(PLAND5A)+scale(STREAM)+scale(PLAND5A)*scale(STREAM),umf)
summary(umf)
DF=data.frame(STREAM=rep(c(10,500),each=30),PLAND5A=seq(1,90,3))
pred=predict(model,type="state",newdata=DF,backTran=FALSE,appendData=FALSE)
abun=exp(pred$Predicted)
LCL=exp(pred$Predicted-(pred$SE*1.4395))
UCL=exp(pred$Predicted+(pred$SE*1.4395))
matrix(data=c(LCL,abun,UCL),ncol=3)

Best,

Matt

Matthew Giovanni

unread,
Jul 3, 2011, 7:49:58 PM7/3/11
to unma...@googlegroups.com
Hi guys.  I have another problem related to the previous discussion about "predict".  I keep getting the same prediction values from my interaction model even though I specify different levels of distance to stream.  For example, I get identical predictions from the two data frames below:


umf=csvToUMF("ACFL2.csv",long=FALSE,type="unmarkedFramePCount")
model=pcount(~1 ~scale(PLAND5A)+scale(STREAM)+scale(PLAND5A)*scale(STREAM),umf)
summary(umf)
DF=data.frame(STREAM=rep(c(10,100),each=9),PLAND5A=seq(1,90,10))
DF=data.frame(STREAM=rep(c(1,500),each=9),PLAND5A=seq(1,90,10))
predict(model,type="state",newdata=DF,backTran=TRUE,appendData=TRUE)

I also modified Richard's code and get identical predictions at different levels of "b":


a <- rnorm(10)
b <- rnorm(10)
y <- matrix(rbinom(30, 1, 0.5), 10, 3)
umf <- unmarkedFramePCount(y=y, siteCovs=data.frame(a=a, b=b))
(fm <- pcount(~1 ~scale(a) * scale(b), umf))
DF <- data.frame(a=-3:3, b=rep(c(-3,3),each=7))
DF <- data.frame(a=-3:3, b=rep(c(-1,3),each=7))

predict(fm, type="state", newdata=DF, backTran=FALSE, appendData=FALSE)

Am I making an obvious mistake here?  Thanks for your input.

Matt

Andy Royle

unread,
Jul 3, 2011, 8:03:13 PM7/3/11
to unma...@googlegroups.com, unma...@googlegroups.com
hi Matt,
 I get the same error running your code. I'll play around with it and see if I can learn something.
regards
andy

 
J. Andy Royle
Research Statistician
USGS Patuxent Wildlife Research Center
12100 Beech Forest Rd.
Laurel, MD 20708
http://profile.usgs.gov/professional/mypage.php?name=aroyle
andy_...@usgs.gov
phone: 301-497-5846
fax: 301-497-5545

Book: "Hierarchical Modeling and Inference in Ecology: The Analysis of Data from Populations, Metapopulations and Communities" by J. A. Royle and R.M. Dorazio.  

unmarked: A very useful R package for fitting certain hierarchical models using likelihood methods. Available from: http://cran.case.edu/web/packages/unmarked/index.html

-----unma...@googlegroups.com wrote: -----

To: unma...@googlegroups.com
From: Matthew Giovanni <matthew...@gmail.com>
Sent by: unma...@googlegroups.com
Date: 07/03/2011 07:49PM

Matthew Giovanni

unread,
Jul 3, 2011, 8:07:16 PM7/3/11
to unma...@googlegroups.com
Thanks, Andy; I really appreciate it.

Matt

Richard Chandler

unread,
Jul 5, 2011, 9:13:48 AM7/5/11
to unma...@googlegroups.com
Hi Matt,

The reason that the predictions were the same using the fake data is that scale(b) is equivalent in the two cases you tried:

> DF1 <- data.frame(a=-3:3, b=rep(c(-3,3),each=7))
> DF2 <- data.frame(a=-3:3, b=rep(c(-1,3),each=7))
> all(scale(DF1$b) == scale(DF2$b))
[1] TRUE

However, I want to recommend to everyone that scale() not be used in formulas until I fix how it is handled. For it to work properly in predict(), scale() needs to use the mean and SD of the *original* covariate, not the one supplied in the newdata data.frame, as it is doing now. So, just standardize before you create the unmarkedFrame, and then make predictions on the transformed scale, and covert back to the original covariate scale using the original mean and SD. Note that other transformations such log(), sqrt(), I(x^2), etc... should work fine.

Richard


_____________________________________
Richard Chandler, post-doc

USGS Patuxent Wildlife Research Center
301-497-5696



From: Matthew Giovanni <matthew...@gmail.com>
To: unma...@googlegroups.com
Date: 07/03/2011 08:08 PM
Subject:
Re: [unmarked] Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 0, 1
Sent by: unma...@googlegroups.com


Marco Silva

unread,
Jun 2, 2020, 6:46:40 AM6/2/20
to unmarked
Hello.
There is a situation that is not commented on in any discussion forum about R and that presents this same error. It might help you. I also spent wasted days trying to solve this.
In my case, the columns with characters from my data.frame were not converted to factors. Therefore, the length of these columns was different from the other columns (numeric variables), generating this error commented on in this post. This prevented me from generating a graph (visreg package) with the results of my GLM. This is because in the latest version of R (4.0.0) the default stringsAsFactors (from the read.table function) have been changed from TRUE to FALSE.
So if this is the case for you, try the following:
read.table (file name, header = T, stringsAsFactors = T)
Hope this helps.
Bye.
Reply all
Reply to author
Forward
0 new messages