Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8016217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:28:47+00:00 2026-06-04T20:28:47+00:00

I can use lm or class::knn to view the source code, but I failed

  • 0

I can use lm or class::knn to view the source code, but I failed to show the code for princomp. Was this function(or something else) written in R or some other bytecode used.
I also could not find the source code using advises from How do I show the source code of an S4 function in a package?. Thanks for any help.

> princomp
function (x, ...) 
UseMethod("princomp")
<bytecode: 0x9490010>
<environment: namespace:stats>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:28:48+00:00Added an answer on June 4, 2026 at 8:28 pm

    You have to ask using the corresponding method used by the function. Try this:

    princomp # this is what you did without having a good enough answer
    methods(princomp) # Next step, ask for the method: 'princomp.default'
    getAnywhere('princomp.default') # this will show you the code
    

    The code you are looking for is:

    function (x, cor = FALSE, scores = TRUE, covmat = NULL, subset = rep(TRUE, 
        nrow(as.matrix(x))), ...) 
    {
        cl <- match.call()
        cl[[1L]] <- as.name("princomp")
        if (!missing(x) && !missing(covmat)) 
            warning("both 'x' and 'covmat' were supplied: 'x' will be ignored")
        z <- if (!missing(x)) 
            as.matrix(x)[subset, , drop = FALSE]
        if (is.list(covmat)) {
            if (any(is.na(match(c("cov", "n.obs"), names(covmat))))) 
                stop("'covmat' is not a valid covariance list")
            cv <- covmat$cov
            n.obs <- covmat$n.obs
            cen <- covmat$center
        }
        else if (is.matrix(covmat)) {
            cv <- covmat
            n.obs <- NA
            cen <- NULL
        }
        else if (is.null(covmat)) {
            dn <- dim(z)
            if (dn[1L] < dn[2L]) 
                stop("'princomp' can only be used with more units than variables")
            covmat <- cov.wt(z)
            n.obs <- covmat$n.obs
            cv <- covmat$cov * (1 - 1/n.obs)
            cen <- covmat$center
        }
        else stop("'covmat' is of unknown type")
        if (!is.numeric(cv)) 
            stop("PCA applies only to numerical variables")
        if (cor) {
            sds <- sqrt(diag(cv))
            if (any(sds == 0)) 
                stop("cannot use cor=TRUE with a constant variable")
            cv <- cv/(sds %o% sds)
        }
        edc <- eigen(cv, symmetric = TRUE)
        ev <- edc$values
        if (any(neg <- ev < 0)) {
            if (any(ev[neg] < -9 * .Machine$double.eps * ev[1L])) 
                stop("covariance matrix is not non-negative definite")
            else ev[neg] <- 0
        }
        cn <- paste("Comp.", 1L:ncol(cv), sep = "")
        names(ev) <- cn
        dimnames(edc$vectors) <- if (missing(x)) 
            list(dimnames(cv)[[2L]], cn)
        else list(dimnames(x)[[2L]], cn)
        sdev <- sqrt(ev)
        sc <- if (cor) 
            sds
        else rep(1, ncol(cv))
        names(sc) <- colnames(cv)
        scr <- if (scores && !missing(x) && !is.null(cen)) 
            scale(z, center = cen, scale = sc) %*% edc$vectors
        if (is.null(cen)) 
            cen <- rep(NA_real_, nrow(cv))
        edc <- list(sdev = sdev, loadings = structure(edc$vectors, 
            class = "loadings"), center = cen, scale = sc, n.obs = n.obs, 
            scores = scr, call = cl)
        class(edc) <- "princomp"
        edc
    }
    <environment: namespace:stats>
    

    I think this what you were asking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As we know, we can use the class EventLog to write event logs, but
You can use Xcode to generate a managed class of an entity. This class
I need to write Comparator for InetSocketAddress so I can use this class in
If I define a new ref class in C++/CLI, can I then use this
In my language I can use a class variable in my method when the
In Ruby 1.9, I can use its class variable like the following: class Sample
How can i use AsyncTask Class to showing dialog when execute a task??? class
Is there any command i can use in activity class that disables TextViews that
How can I use an inherited class of a triangulation in the context of
With CodeIgniter you can use the built in form validation class, which basically allows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.