pspp-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: means procedure


From: Alan Mead
Subject: Re: means procedure
Date: Sun, 25 Jan 2015 22:55:04 -0600
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0

On 1/25/2015 4:38 PM, ftr wrote:
> Does MEANS allow to do what I want (or another procedure) ?

As far as I know, not in SPSS and therefore not in PSPP.  I think that
you can get this from the factorial ANOVA (UNIANOVA?) output, but that's
not implemented in PSPP.

FWIW, writing a script to calculate this wouldn't be hard and below is a
short Perl script. By using a hash to accumulate sums for factors and to
accumulate N's (my %means; my %ns;), it's easily extensible to more
factors and it doesn't care how many levels. The first iteration below
just outputs the cells.  I then re-wrote the output to make a table but
that version is almost twice as long and will only do 2x2 tables.  The
scripts and two little data files are attached in a ZIP file (unzip with
'-a -aa' on *ix, or use dos2unix to convert, because the files are in
DOS format).

-Alan

$ cat data.dat
dv1,var1,var2
0,1,1
1,1,2
0,2,1
0,2,2
0,1,1
1,1,2
0,2,1
0,2,2

$ cat means.pl
#!/usr/bin/perl

use warnings;
use strict;

my $fn = shift || 'data.dat';
my $DELIM = ',';
my $HEADER_TARGET = 'DV1,Var1,Var2'; #sync changes with split on line 20

open( my $fh, '<', $fn) or die( "Cannot open \"$fn\": $!\n");
my $header = <$fh>;
$header =~ s/[\r\n]+//g; # chomp any newlines and only newlines
die("The header should be: \"$HEADER_TARGET\", not \"$header\"\n")
   unless( lc $header eq lc $HEADER_TARGET);

my %ns; # holds n's
my %means; # holds means
while( my $line = <$fh> ) {
  $line =~ s/[\r\n]+//g; # chomp any newlines and only newlines
  $line =~ s/ +//g unless( $DELIM eq ' '); # remove spaces
  my($dv,$fac1,$fac2) = split( /$DELIM/, $line); # sync with
$HEADER_TARGET line 8
  $means{"fac1=$fac1"} += $dv;
  $means{"fac2=$fac2"} += $dv;
  $means{"fac1=$fac1, fac2=$fac2"} += $dv;
  $means{"grand"} += $dv;
  $ns{"fac1=$fac1"}++;
  $ns{"fac2=$fac2"}++;
  $ns{"fac1=$fac1, fac2=$fac2"}++;
  $ns{"grand"}++;
}

printf "%8s  %8s  %s\n", 'means', 'N', 'which';
for my $val ( sort keys %means ) {
  printf "%8.3f  %8d  %s\n", $means{$val}/$ns{$val}, $ns{$val}, $val;
}

$ ./means.pl
   means         N  which
   0.500         4  fac1=1
   0.000         2  fac1=1, fac2=1
   1.000         2  fac1=1, fac2=2
   0.000         4  fac1=2
   0.000         2  fac1=2, fac2=1
   0.000         2  fac1=2, fac2=2
   0.000         4  fac2=1
   0.500         4  fac2=2
   0.250         8  grand
$ cat means2.pl
#!/usr/bin/perl

use warnings;
use strict;

my $fn = shift || 'data.dat';
my $DELIM = ',';
my $HEADER_TARGET = 'DV1,Var1,Var2'; #sync changes with split on line 20

open( my $fh, '<', $fn) or die( "Cannot open \"$fn\": $!\n");
my $header = <$fh>;
$header =~ s/[\r\n]+//g; # chomp any newlines and only newlines
die("The header should be: \"$HEADER_TARGET\", not \"$header\"\n")
   unless( lc $header eq lc $HEADER_TARGET);

my %ns; # holds n's
my %means; # holds means
my %fac1_levels;
my %fac2_levels;
while( my $line = <$fh> ) {
  $line =~ s/[\r\n]+//g; # chomp any newlines and only newlines
  $line =~ s/ +//g unless( $DELIM eq ' '); # remove spaces
  my($dv,$fac1,$fac2) = split( /$DELIM/, $line); # sync with
$HEADER_TARGET line 8
  $means{"fac1=$fac1"} += $dv;
  $means{"fac2=$fac2"} += $dv;
  $means{"fac1=$fac1, fac2=$fac2"} += $dv;
  $means{"grand"} += $dv;
  $ns{"fac1=$fac1"}++;
  $ns{"fac2=$fac2"}++;
  $ns{"fac1=$fac1, fac2=$fac2"}++;
  $ns{"grand"}++;
  $fac1_levels{$fac1}++;
  $fac2_levels{$fac2}++;
}

#printf "%8s  %8s  %s\n", 'means', 'N', 'which';
#for my $val ( sort keys %means ) {
#  printf "%8.3f  %8d  %s\n", $means{$val}/$ns{$val}, $ns{$val}, $val;
#}

# print headers
print ' ' x 12, "var 1\n";
printf "%8s", 'var2';
for my $f1 ( sort keys %fac1_levels ) {
  printf "%8s","lev $f1";
}
printf "%8s\n","total";

# print body
for my $f2 ( sort keys %fac2_levels ) {
  printf "%8s", "lev $f2";
  for my $f1 ( sort keys %fac1_levels ) {
    my $cell = "fac1=$f1, fac2=$f2";
    printf "%8.3f",$means{$cell}/$ns{$cell};
  }
  printf "%8.3f\n",$means{"fac2=$f2"}/$ns{"fac2=$f2"};
}

# print last row
printf "%8s", "total";
for my $f1 ( sort keys %fac1_levels ) {
  printf "%8.3f",$means{"fac1=$f1"}/$ns{"fac1=$f1"};
}
printf "%8.3f\n",$means{'grand'}/$ns{'grand'};

$ ./means2.pl
            var 1
    var2   lev 1   lev 2   total
   lev 1   0.000   0.000   0.000
   lev 2   1.000   0.000   0.500
   total   0.500   0.000   0.250

$ cat dat2.dat
dv1,var1,var2
0,1,1
1,1,2
0,2,1
0,2,2
0,1,1
1,1,2
0,2,1
0,2,2
0,3,1
0,3,2
0,3,1
1,3,2

$ ./means2.pl dat2.dat
            var 1
    var2   lev 1   lev 2   lev 3   total
   lev 1   0.000   0.000   0.000   0.000
   lev 2   1.000   0.000   0.500   0.500
   total   0.500   0.000   0.250   0.250






-- 

Alan D. Mead, Ph.D.
President, Talent Algorithms Inc.

science + technology = better workers

+815.588.3846 (Office)
+267.334.4143 (Mobile)

http://www.alanmead.org

Announcing the Journal of Computerized Adaptive Testing (JCAT), a
peer-reviewed electronic journal designed to advance the science and
practice of computerized adaptive testing: http://www.iacat.org/jcat

Attachment: means_script.zip
Description: Zip compressed data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]