2013-05-12 3 views
6

テーブルパッケージのtabular()関数を使用して2つの変数のクロス集計を行いたい例えばv1とv2)、chisq-testのp値をテーブルに提示する。クロス集計をするのは簡単ですが、テーブルの中でp値を得ることはできません。これは私がすべての運なしに、しようとしてきたものです:R/Knitr/Rstudioのテーブルパッケージを使用したラテックステーブル内のテスト統計情報(例えばchisquareテスト)

enter image description here

\documentclass{article} 

\begin{document} 

<<echo=TRUE,message=FALSE>>= 
library(Hmisc) 
library(tables) 
v1 <- sample(letters[1:2],200,replace=TRUE) 
v2 <- sample(month.name[1:3],200,replace=TRUE) 
df <- data.frame(v1,v2) 
@ 

It is straight forward to get the crosstabulation: 
<<results='asis'>>= 
latex( tabular( Factor(v1) ~ Factor(v2)  , data=df) ) 
@ 

But I cant get the p-value inside the table: 

<<results='asis'>>= 
latex( tabular( Factor(v1)*chisq.test(v1,v2)$p.value ~ Factor(v2)  , data=df) ) 
@ 

\end{document} 
+0

多分、[reporttools](http://cran.r-project.org/web/packages/reporttools/index.html)パッケージを使用できます。 – sgibb

答えて

7

私はtables::tabularでどのように行うのか分からないが、これはあなたがあなたのシステムを設定したと仮定しHmisc::summary.formula.reverseでそれを行いますlatex()を通してpdfファイルを生成します。私はRhelpのアーカイブを検索して、 'exclude1'引数がlatex引数リストに入る必要があることを理解しなければなりませんでした。

library(Hmisc) 
latex(summary(v2 ~ v1, data=df, method="reverse" ,test=TRUE), exclude1=FALSE) 

enter image description here

あなたが一緒に「ラテックス出力を傍受することができます:あなたが戻ってドキュメントexclude1通過したら、私はは私がsummary.rmsのヘルプページを読んでいたと思ったが、latex.summary.formula.reverseの使用例に表示されませんあなたが指定したファイルに出力を割り当てることで長いドキュメントに埋め込みたい場合は、「方法」を選択します。

latex(summary(v2 ~ v1, data=df, method="reverse" ,test=TRUE), exclude1=FALSE, file="") 
#-------- 
% latex.default(cstats, title = title, caption = caption, rowlabel = rowlabel,  col.just = col.just, numeric.dollar = FALSE, insert.bottom = legend,  rowname = lab, dcolumn = dcolumn, extracolheads = extracolheads,  extracolsize = Nsize, ...) 
% 
\begin{table}[!tbp] 
\caption{Descriptive Statistics by v2\label{summary}} 
\begin{center} 
\begin{tabular}{lcccc} 
\hline\hline 
\multicolumn{1}{l}{}&\multicolumn{1}{c}{February}&\multicolumn{1}{c}{January}&\multicolumn{1}{c}{March}&\multicolumn{1}{c}{Test Statistic}\tabularnewline 
&\multicolumn{1}{c}{{\scriptsize $N=56$}}&\multicolumn{1}{c}{{\scriptsize $N=73$}}&\multicolumn{1}{c}{{\scriptsize $N=71$}}&\tabularnewline 
\hline 
v1~:~a&43\%~{\scriptsize~(24)}&47\%~{\scriptsize~(34)}&44\%~{\scriptsize~(31)}&$ \chi^{2}_{2}=0.21 ,~ P=0.901 $\tabularnewline 
~~~~b&57\%~{\scriptsize~(32)}&53\%~{\scriptsize~(39)}&56\%~{\scriptsize~(40)}&\tabularnewline 
\hline 
\end{tabular} 
\end{center} 
Numbers after percents are frequencies.\\\noindent Test used:\\Pearson test\end{table} 
+0

私はそれに感謝します。 –

+0

RstudioのKnitrでどうやったらうまくいくのですか?結果を「asis」-chunkに入れて、Rstudioがハングアップするだけです(「ハング」によって):latex(summary(v2〜v1、data = df、method = "reverse"、test = TRUE)、exclude1 = FALSE)つまり、「コンパイル-pdf」タブの右下隅に「停止」記号が表示され、他のチャンクを実行する前に「停止」をクリックする必要があります。 –

+1

私はRStudioでもknitrでもない私は "asis"チャンクの使い方に慣れていません。RStudio環境内で出力を保持するかもしれないfile = ""を指定することで、 "生の"ラテックス出力をコンソールに得ることができます。 –

0

また、カイ二乗統計からのテキストをxtable()でキャプションに貼り付けることもできます。例:

#sample data 
var1<-sample(c('A', 'B'), 10, replace=T) 
var2<-sample(c('Red', 'Blue'), 10, replace=T) 
#join in frequency table 
tab<-table(var1, var2) 
#conduct chisq.test 
test<-chisq.test(tab) 
#show values of chisq.test() 
name(test) 
#Use xtable, use print.xtable for further manipulations 
out<-xtable(tab, caption=paste('Important table, chi-squared =', test$statistic, ', p=', test$p.value,',' ,test$parameter, 'df', sep=' ')) 
#print 
out 
関連する問題