[R] princomp - how to take out part result
David Winsemius
dwinsemius at comcast.net
Sat Feb 7 16:43:01 CET 2009
On Feb 7, 2009, at 9:07 AM, Nash wrote:
> Dear all, i have two problems
>
> if i have a principal components analysis report as follows:
>
> ?princomp
>
> problem one.
>
>> pc.cr <- princomp(USArrests, cor = TRUE)
>> pc.cr
>
> i want to export a txt file (*.txt) to save "pc.cr" report
> ###########################################
> Call:
> princomp(x = USArrests, cor = TRUE)
>
> Standard deviations:
> Comp.1 Comp.2 Comp.3 Comp.4
> 1.5748783 0.9948694 0.5971291 0.4164494
>
> 4 variables and 50 observations.
> ###########################################
> what can i do ?
?sink
>
>
> -----------------------------------------------------------
> problem two.
>
>> load <- loadings(pc.cr)
>> load
>
> Loadings:
> Comp.1 Comp.2 Comp.3 Comp.4
> Murder -0.536 0.418 -0.341 0.649
> Assault -0.583 0.188 -0.268 -0.743
> UrbanPop -0.278 -0.873 -0.378 0.134
> Rape -0.543 -0.167 0.818
>
> Comp.1 Comp.2 Comp.3 Comp.4
> SS loadings 1.00 1.00 1.00 1.00
> Proportion Var 0.25 0.25 0.25 0.25
> Cumulative Var 0.25 0.50 0.75 1.00
>
> i want to take out matrix
>
> Comp.1 Comp.2 Comp.3 Comp.4
> SS loadings 1.00 1.00 1.00 1.00
> Proportion Var 0.25 0.25 0.25 0.25
> Cumulative Var 0.25 0.50 0.75 1.00
>
> what can i do ?
When you look at load with str(), you only see the loadings matrix and
not the rest of what you thought was a "result", which means to me
that the print method for that class of object is doing something
behind the scenes. See the method thusly:
> getAnywhere(print.loadings)
A single object matching ‘print.loadings’ was found
It was found in the following places
registered S3 method for print from namespace stats
namespace:stats
with value
function (x, digits = 3, cutoff = 0.1, sort = FALSE, ...)
{
Lambda <- unclass(x)
p <- nrow(Lambda)
factors <- ncol(Lambda)
if (sort) {
mx <- max.col(abs(Lambda))
ind <- cbind(1:p, mx)
mx[abs(Lambda[ind]) < 0.5] <- factors + 1
Lambda <- Lambda[order(mx, 1:p), ]
}
cat("\nLoadings:\n")
fx <- format(round(Lambda, digits))
names(fx) <- NULL
nc <- nchar(fx[1], type = "c")
fx[abs(Lambda) < cutoff] <- paste(rep(" ", nc), collapse = "")
print(fx, quote = FALSE, ...)
vx <- colSums(x^2)
varex <- rbind(`SS loadings` = vx)
if (is.null(attr(x, "covariance"))) {
varex <- rbind(varex, `Proportion Var` = vx/p)
if (factors > 1)
varex <- rbind(varex, `Cumulative Var` = cumsum(vx/p))
}
cat("\n")
print(round(varex, digits))
invisible(x)
}
<environment: namespace:stats>
So vx = colSums(x^2); "SS loadings" = vx, "Proportion Var" = vx/p, and
`Cumulative Var` = cumsum(vx/p).
You should be able to take it from there, since presumably you can
work with the x matrix which is really just "load" by another name.
> x <- load
> colSums(x^2)
Comp.1 Comp.2 Comp.3 Comp.4
1 1 1 1
--
David Winsemius
>
>
> Can anyone help me ?
>
> Nash 2009.02.07
>
>
>
>
>
> --
> Nash - morrison at ibms.sinica.edu.tw
>
> ______________________________________________
> 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.
More information about the R-help
mailing list