pcp                 package:hddplot                 R Documentation

_c_o_n_v_e_n_i_e_n_c_e _v_e_r_s_i_o_n _o_f _t_h_e _s_i_n_g_u_l_a_r _v_a_l_u_e _d_e_c_o_m_p_o_s_i_t_i_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     Packages results from an SVD on what can be either a cases by
     variables (features) or variables by cases layout, for use in
     principal component and related calculations

_U_s_a_g_e:

     pcp(x = USArrests, varscores = TRUE, cases = "rows", center = "vars", standardize = FALSE, scale.cases = 1, log = FALSE, sc = 1, reflect = c(1, 1))

_A_r_g_u_m_e_n_t_s:

       x: matrix on which SVD is to be performed

varscores: logical; should scores be returned?

   cases: specify either '"rows"' or '"columns"'

  center: logical: if set to '"vars"', then values of variables will be
          centered

standardize: logical: should values of variables be standardized to
          zero mean and unit deviance.  Takes precedence over the
          setting of  'center'

scale.cases: set to a value in [0,1]. 'scale.cases=0' gives  a pure
          rotation of the variables. 'scale.cases=1' weights a/c the 
          singular values

     log: logical: should logarithms be taken, prior to the
          calculation?

      sc: the variable scores are divided by $\sqrt{sc-1}$. By default,
          'sc' = number of cases

 reflect: a vector of two elements, by default 'c(1,1)'.  Use of -1 in
          one or both positions can be useful in reconciling results
          with output from other software

_D_e_t_a_i_l_s:

_V_a_l_u_e:

       g: case scores

       h: variable scores

     avv: variable means

    sdev: singular values, divides by the square root of one less than
          the number of cases

_N_o_t_e:

_A_u_t_h_o_r(_s):

     John Maindonald

_R_e_f_e_r_e_n_c_e_s:

_S_e_e _A_l_s_o:

     'La.svd'

_E_x_a_m_p_l_e_s:

     USArrests.svd <- pcp(x = USArrests)

     ## The function is currently defined as
     function(x=USArrests,
                varscores=TRUE,
                cases="rows",
                center="vars",
                standardize=FALSE,
                scale.cases=1,
                log=FALSE,
                sc=1,
                reflect=c(1,1))
     {
       x <- as.matrix(x)
       avv <- 0
       sdv <- 1
       casedim <- 2-as.logical(cases=="rows")
       vardim <- 3-casedim
       ## casedim=1 if rows are cases; otherwise casedim=2
       ## scale.cases=0 gives a pure rotation of the variables
       ## scale.cases=1 weights a/c the singular values
       ncases <- dim(x)[casedim]
       nvar <- dim(x)[vardim]
       if(is.null(sc))sc <- dim(x)[casedim]-1
       if(log)x <- log(x, base=2)
       if(standardize){
         avv <- apply(x, vardim, mean)
         sdv <- apply(x, vardim, sd)        
         x <- sweep(x, vardim, avv,"-")
         x <- sweep(x, vardim, sdv,"/")
       }
       else if(as.logical(match("vars", center, nomatch=0))){
         avv <- apply(x,vardim, mean)
         x <- sweep(x, vardim, avv,"-")}

       svdx <- La.svd(x, method = c("dgesdd"))
       h <- NULL
       if(cases=="rows"){
         g <- sweep(svdx$u, 2, svdx$d^scale.cases, "*")*sqrt(sc)
         if(varscores)
           h <- t((svdx$d^(1-scale.cases)* svdx$vt ))/sqrt(sc)
       }
       else if(cases=="columns"){
         g <- sweep(t(svdx$vt), 2, svdx$d^scale.cases, "*")*sqrt(sc)
         if(varscores)
           h <- sweep(svdx$u, 2, svdx$d^(1-scale.cases),"*")/sqrt(sc)
       }
       invisible(list(g=g, rotation=h, av=avv, sdev=svdx$d/sqrt(ncases-1)))
       }

