Skip to content

Apply acf to data frame containing 'NA'

6 messages · Zilefac Elvis, arun, Hannah Barron +1 more

#
Hi,
May be this helps:
set.seed(24)
A <- matrix(sample(c(NA,rnorm(1500)),1500,replace=FALSE),nrow=500)
par(mfrow=c(ncol(A),1))
?lapply(split(A,col(A)),function(x) acf(na.omit(x)))
A.K.




----- Original Message -----
From: Zilefac Elvis <zilefacelvis at yahoo.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Sunday, June 23, 2013 1:29 PM
Subject: [R] Apply acf  to data frame containing 'NA'

Hi,
I have a data frame with each column representing a?separate site.
Following this code, I can apply acf to all columns in A. 'A' contains randomly generated data.

A <- matrix(rnorm(1500),nrow=500)
par(mfrow=c(ncol(A),1))

lapply(split(A,col(A)), acf)

#OR
lapply(split(A,col(A)), function(snow) acf(snow, lag.max=5))


Problem:?Some of my data contains 'NA' in some of the columns. I get this error
"Error in na.fail.default(as.ts(x)) : missing values in object"
How can I resolve this problem?

Thanks.
AE.
??? [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
Hi,
Thanks so much for the quick reply.
It worked on your data but failed on mine.
Attached is my data.?
I received this error:
"Error in acf(na.omit(x)) : 'lag.max' must be at least 0
In addition: Warning message:
In split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) :
? data length is not a multiple of split variable"

AE.
#
HI,
dat1<- read.csv("AMS3.csv",sep="\t",row.names=1)
pdf("AMS3_acf.pdf")
?lapply(dat1,function(x)acf(na.omit(x)))
dev.off()

A.K.
#
1. acf already has a na.action argument which should be used instead
of the acf(na.omit(x)) construction.

2. But more to the point, acf(x,na=na.omit)  may very well be complete
nonsense (see ?acf for hints at this). How to handle estimate
covariances with missingness and, more particularly, how to compute
acf's with missings is no easy matter.  I would recommend time with
the literature and/or consulting with a local expert before plunging
into code that, whether or not it runs, can produce wrong results.

Cheers,
Bert
On Sun, Jun 23, 2013 at 11:00 AM, arun <smartpink111 at yahoo.com> wrote: