Discussion:
[Rd] prcomp: problem with zeros? (PR#8870)
juha.heljoranta
2006-05-17 18:42:43 UTC
Permalink
Full_Name: Juha Heljoranta
Version: R 2.1.1 (2005-06-20)
OS: Gentoo Linux
Submission from: (NULL) (88.112.29.250)


prcomp has a bug which causes following error

Error in svd(x, nu = 0) : infinite or missing values in 'x'

on a valid data set (no Infs, no missing values). The error is most likely
caused by the zeros in data.

My code and temporary workaround:


m = matrix(...
...
prcomp(m, center = TRUE, scale = TRUE)
Error in svd(x, nu = 0) : infinite or missing values in 'x'


m = matrix(...
...
# ugly work around
m = m + 1e-120
# too small values will not work
# m = m + 1e-150
prcomp(m, center = TRUE, scale = TRUE)
# success


The matrix in question is ~1024x13000 containing double values, thus totaling of
~103M of raw data. I can put it online if needed.
ripley
2006-05-17 19:02:40 UTC
Permalink
Post by juha.heljoranta
Full_Name: Juha Heljoranta
Version: R 2.1.1 (2005-06-20)
Not a current version of R.
Post by juha.heljoranta
OS: Gentoo Linux
Submission from: (NULL) (88.112.29.250)
prcomp has a bug which causes following error
Error in svd(x, nu = 0) : infinite or missing values in 'x'
on a valid data set (no Infs, no missing values). The error is most likely
caused by the zeros in data.
Why do you say that? Without a reproducible example, we cannot judge what
is going on. If you called prcomp with scale=TRUE on a matrix that has a
completely zero (or constant) column, then this is a reasonable error
message.
Post by juha.heljoranta
m = matrix(...
...
prcomp(m, center = TRUE, scale = TRUE)
Error in svd(x, nu = 0) : infinite or missing values in 'x'
m = matrix(...
...
# ugly work around
m = m + 1e-120
# too small values will not work
# m = m + 1e-150
prcomp(m, center = TRUE, scale = TRUE)
# success
The matrix in question is ~1024x13000 containing double values, thus totaling of
~103M of raw data. I can put it online if needed.
______________________________________________
R-devel at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
Jari Oksanen
2006-05-17 19:25:53 UTC
Permalink
Post by ripley
Post by juha.heljoranta
prcomp has a bug which causes following error
Error in svd(x, nu = 0) : infinite or missing values in 'x'
on a valid data set (no Infs, no missing values). The error is most likely
caused by the zeros in data.
Why do you say that? Without a reproducible example, we cannot judge what
is going on. If you called prcomp with scale=TRUE on a matrix that has a
completely zero (or constant) column, then this is a reasonable error
message.
Constant columns (which is a likely reason here) indeed become NaN
after scale(), but the error message was:

Error in svd(x, nu = 0) : infinite or missing values in 'x'

and calling this 'reasonable' is stretching the limits of reason.

However, in general this is "easy" to solve: scale() before the
analysis and replace NaN with 0 (prcomp handles zeros). For instance,

x <- scale(x)
x[is.nan(x)] <- 0
prcomp(x)

(and a friendly prcomp() would do this internally.)

cheers, jari oksanen
--
Jari Oksanen, Oulu, Finland
juha.heljoranta
2006-05-18 06:08:19 UTC
Permalink
Post by ripley
Post by juha.heljoranta
Error in svd(x, nu = 0) : infinite or missing values in 'x'
Why do you say that? Without a reproducible example, we cannot judge
what is going on. If you called prcomp with scale=TRUE on a matrix that
has a completely zero (or constant) column, then this is a reasonable
error message.
My bad, the matrix has actually a zero column.

Thank you for your time and sorry for any inconveniences that this may
have caused.

Regards,
Juha Heljoranta

Loading...