[R] Principal Components for matrices with NA

282 views
Skip to first unread message

Joyous Fisher

unread,
Feb 27, 2012, 2:30:58 PM2/27/12
to r-h...@r-project.org
Hello,

I have a matrix with 267 columns, all rows of which have at least one
column missing (NA).
All three methods i've tried (pcs, princomp, and prcomp) fail with either

"Error in svd(zsmall) : infinite or missing values in 'x'" (latter two)

or

"Error in cov.wt(z) : 'x' must contain finite values only"

The last one happens because of the check

if (!all(is.finite(x)))

in cov.wt

Q: is there a way to do princomp or another method where every row has at
least one missing column?

I guess if missing values are thrown out, that leaves me with a zero row
matrix.
I could find the maximal set of columns such that there exists a subset of
rows with non NA values for every column in the set - what is an efficient
way to do that?

Kind Regards
JS

[[alternative HTML version deleted]]

______________________________________________
R-h...@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.

Rui Barradas

unread,
Feb 27, 2012, 3:57:07 PM2/27/12
to r-h...@r-project.org
Hello,

> I could find the maximal set of columns such that there exists a subset of
> rows with non NA values for every column in the set - what is an
> efficient
> way to do that?

Try 'na.exclude' on the transpose matrix.
Example:

set.seed(1)
x <- matrix(1:200, ncol=25)

f <- function(x){x[sample(length(x), 1)] <- NA; x}
x <- t(apply(x, 1, f))
x

x.without.NA <- t(na.exclude(t(x)))

Hope this helps,

Rui Barradas

--
View this message in context: http://r.789695.n4.nabble.com/Principal-Components-for-matrices-with-NA-tp4425930p4426040.html
Sent from the R help mailing list archive at Nabble.com.

Rui Barradas

unread,
Feb 27, 2012, 5:17:21 PM2/27/12
to r-h...@r-project.org
Hello again,

> Q: is there a way to do princomp or another method where every row has at
> least one missing column?

See also package 'psych', function 'principal'. You can impute mean or
median to NAs.

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/Principal-Components-for-matrices-with-NA-tp4425930p4426284.html


Sent from the R help mailing list archive at Nabble.com.

______________________________________________

Mark Difford

unread,
Feb 28, 2012, 1:43:45 AM2/28/12
to r-h...@r-project.org
On Feb 27, 2012 at 9:30pm Joyous Fisher wrote:

> Q: is there a way to do princomp or another method where every row has at
> least one missing column?

You have several options. Try function nipals in packages ade4 and plspm.
Also look at package pcaMethods (on Bioconductor), where you will find a
full range of options for carrying out principal component analysis using
matrices with missing values.

Regards, Mark.

-----
Mark Difford (Ph.D.)
Research Associate
Botany Department
Nelson Mandela Metropolitan University
Port Elizabeth, South Africa
--
View this message in context: http://r.789695.n4.nabble.com/Principal-Components-for-matrices-with-NA-tp4425930p4427216.html


Sent from the R help mailing list archive at Nabble.com.

______________________________________________

Reply all
Reply to author
Forward
0 new messages