我想制作一个列联表。我找到了代码这里但我想做几点修改。
我怎样才能使数字不采用科学计数法并省略最后一行?我根据疾病状态进行调节,因此列总数不是我感兴趣的。
我使用的代码如下所示。
\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{mathtools}
\usepackage{float}
\usepackage{tabularx}
\usepackage{array}
\usepackage{wrapfig}
\usepackage{multirow}
\usepackage{tabu}
\usepackage{booktabs}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\newcommand{\numpy}{{\tt numpy}} % tt font for numpy
\newcommand{\createcontingencytable}[4]{ %
% #1=table name
% #2=first column name
% #3=new row sum name
% #4=new column sum name
\pgfplotstablecreatecol[
create col/assign/.code={% In each row ...
\def\rowsum{0}
\pgfmathtruncatemacro\maxcolindex{\pgfplotstablecols-1}
% ... loop over all columns, summing up the elements
\pgfplotsforeachungrouped \col in {1,...,\maxcolindex}{
\pgfmathsetmacro\rowsum{\rowsum+\thisrowno{\col}}
}
\pgfkeyslet{/pgfplots/table/create col/next content}\rowsum
}
]{#3}{#1}%
%
% Transpose the table, so we can repeat the summation step for the columns
\pgfplotstabletranspose[colnames from={#2},input colnames to={#2}]{\intermediatetable}{#1}
%
% Sums for each column
\pgfplotstablecreatecol[
create col/assign/.code={%
\def\colsum{0}
\pgfmathtruncatemacro\maxcolindex{\pgfplotstablecols-1}
\pgfplotsforeachungrouped \col in {1,...,\maxcolindex}{
\pgfmathsetmacro\colsum{\colsum+\thisrowno{\col}}
}
\pgfkeyslet{/pgfplots/table/create col/next content}\colsum
}
]{#4}\intermediatetable
%
% Transpose back to the original form
\pgfplotstabletranspose[colnames from=#2, input colnames to=#2]{\contingencytable}{\intermediatetable}
}
%
\topmargin -.5in
\textheight 9in
\oddsidemargin -.25in
\evensidemargin -.25in
\textwidth 7in
\begin{document}
\begin{center}
\pgfplotstableread{
$X$(Disease) Positive Negative
Yes 0.95 0.05
No 0.05 0.95
}\chisquaredata
\createcontingencytable{\chisquaredata}{$X$(Disease)}{Total}{Total}
\pgfplotstabletypeset[
every head row/.style={%
before row={\toprule
& \multicolumn{2}{c}{$Y$(Diagnosis)}\\ \cmidrule{2-4}},
after row=\midrule},
every last row/.style={after row=\bottomrule},
columns/$X$(Disease)/.style={string type},
columns={$X$(Disease), Positive, Negative, {Total}},
]\contingencytable
\end{center}
\end{document}
\grid
\grid
答案1
pgrplotstable
我建议不要使用,而是采用以下方法手动计算总数:
\documentclass[11pt]{article}
\usepackage{booktabs}
\begin{document}
\begin{center}
\begin{tabular}{cccc}
\toprule
& \multicolumn{3}{c}{$Y$ (Diagnosis)} \\
\cmidrule{2-4}
$X$(Disease) & Positive & Negative & Total \\
\midrule
Yes & 0.95 & 0.05 & 1 \\
No & 0.05 & 0.95 & 1 \\
\bottomrule
\end{tabular}
\end{center}
\end{document}