SPSS:由于数字四舍五入导致总数不一致

SPSS: Inconsistent totals due to rounding of numbers

我在 运行 使用 SPSS 自定义 table 的数据时使用权重。

因此,由于小数点四舍五入,预计列值或行值加起来可能不等于行总计、列总计或 Table 总计

样本table结果:

                                  variable 2
                         category 1       category 2      Total
variable 1   category 1       45             52             97
             category 2       60             56             115
             Total           105            107             211

有没有办法强制 SPSS 输出正确的行、列或 table 总数?

预期table输出:

                                  variable 2
                         category 1       category 2      Total
variable 1   category 1       45             52             97
             category 2       60             56             116
             Total           105            108             213

如果您正在使用 CROSSTABS procedure to produce these figures then you should do using the option ASIS

需要说明的是:CTABLES 显示的总数在数学上是正确的。但是,如果您想将行中显示值的总和显示为总计,唯一的方法是使用 STATS TABLE CALC 扩展命令使用四舍五入的值重新计算总计。

这是如何做到这一点。 首先,您需要创建一个名为 customcalc.py 的 Python 模块,其内容如下

def custom(datacells, ncells, roworcol):  
    '''Calculate sum of formatted values'''  
    total = sum(float(datacells.GetValueAt(roworcol,i)) for i in range(ncells))  
    return(total)

此文件应保存在 Statistics 安装目录下的 python\lib\site-packages 目录中或 Python 可以找到的其他任何地方。

然后,在你的 CTABLES 命令之后,运行 这个语法

STATS TABLE CALC SUBTYPE="customtable" PROCESS=PRECEDING  
/TARGET custommodule="customcalc"  
FORMULA="customcalc.custom(datacells, ncells, roworcol)" DIMENSION=COLUMNS LEVEL = -2  LOCATION="Total"   
LABEL="Rounded Count". 

该自定义函数将每行中的格式化值相加,而不是全精度值。如果您已抑制默认统计名称 Count,因此 "Total" 是最里面的标签,请使用 LEVEL=-1 而不是 LEVEL=-2 ABOVE。