【问题标题】:Using xtable function on candisc::cancor output在 candisc::cancor 输出上使用 xtable 函数
【发布时间】:2014-11-02 00:43:55
【问题描述】:

我想在candisc::cancor 输出上使用xtable 函数。但是想不通。

library(xtable)
library(candisc)
data(Rohwer, package="heplots")
X <- as.matrix(Rohwer[,6:10])  # the PA tests
Y <- as.matrix(Rohwer[,3:5])   # the aptitude/ability variables
(cc <- cancor(X, Y, set.names=c("PA", "Ability")))
Canonical correlation analysis of:
         5   PA  variables:  n, s, ns, na, ss 
  with   3   Ability  variables:  SAT, PPVT, Raven 

    CanR  CanRSQ   Eigen percent    cum                          scree
1 0.6703 0.44934 0.81599   77.30  77.30 ******************************
2 0.3837 0.14719 0.17260   16.35  93.65 ******                        
3 0.2506 0.06282 0.06704    6.35 100.00 **                            

Test of H0: The canonical correlations in the 
current row and all that follow are zero

     CanR  WilksL      F df1   df2  p.value
1 0.67033 0.44011 3.8961  15 168.8 0.000006
2 0.38366 0.79923 1.8379   8 124.0 0.076076
3 0.25065 0.93718 1.4078   3  63.0 0.248814
xtable(cc)

Error in UseMethod("xtable") : 
  no applicable method for 'xtable' applied to an object of class "cancor"

【问题讨论】:

    标签: r knitr sweave xtable rnw


    【解决方案1】:

    如果您查看methods(xtable),则没有cancor 对象的方法,但我们希望可以使用xtable.data.frame,因为cancor 的输出看起来像data.frames

    但是,cancor 不会返回这些值 - 请查看 str(cc)。它们是在print 方法中计算的。看candisc:::print.cancor

    所以创建一个快速提取函数来生成cancor 输出为data.frames

    outfun <- function(X) { 
                  CanR = X$cancor  
                  CanRSQ = CanR^2  
                  Eigen = CanRSQ/(1 - CanRSQ)
                  percent = 100 * Eigen/sum(Eigen)
                  cum = cumsum(percent)
                  list(tab1 = data.frame(CanR, CanRSQ, Eigen, percent, cum) ,
                       tab2 = as.data.frame(candisc:::Wilks.cancor(X)))
            }
    

    运行辅助函数

    (out <- outfun(cc))
    
    # or run it like
    out <- outfun(cancor(X, Y, set.names=c("PA", "Ability")))
    

    然后可以使用xtable.data.frame 生成xtable 输出

    xtable:::xtable.data.frame(out[[1]])
    
    # % latex table generated in R 3.1.1 by xtable 1.7-3 package
    # % Wed Sep 17 18:32:07 2014
    # \begin{table}[ht]
    # \centering
    # \begin{tabular}{rrrrrr}
    #   \hline
    #  & CanR & CanRSQ & Eigen & percent & cum \\ 
    #   \hline
    # 1 & 0.67 & 0.45 & 0.82 & 77.30 & 77.30 \\ 
    #   2 & 0.38 & 0.15 & 0.17 & 16.35 & 93.65 \\ 
    #   3 & 0.25 & 0.06 & 0.07 & 6.35 & 100.00 \\ 
    #    \hline
    # \end{tabular}
    # \end{table}
    
    xtable:::xtable.data.frame(out[[2]])
    

    您可以调整辅助函数以提取标题等,然后可以将其作为参数传递给xtable.data.frame

    一个可以帮助您入门的快速答案

    【讨论】:

    • 非常感谢您的帮助并提供了非常有用的答案。非常感激。谢谢
    • 需要在as.data.frame(candisc:::Wilks.cancor(cc)) 中做一些更改,因为cc 没有定义。
    • 干杯,再次忘记在发布之前清除我的工作区;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    相关资源
    最近更新 更多