File IO and Statistical Examples


GNU Octave, version 2.1.33 (i386-redhat-linux-gnu).
Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 John W. Eaton.
This is free software with ABSOLUTELY NO WARRANTY.
For details, type `warranty'.

*** This is a development version of Octave.  Development releases
*** are provided for people who want to help test, debug, and improve
*** Octave.
***
*** If you want a stable, well-tested version of Octave, you should be
*** using one of the stable releases (when this development release
*** was made, the latest stable version was 2.0.16).

octave:1> ls
diary  file_io.data  junk  session  simple_octave.html
octave:2> dir
diary  file_io.data  junk  session  simple_octave.html
octave:3> 
octave:3> # load the file data        
octave:3>  load file_io.data      
octave:4>  file_io 
file_io =

    4.50000    8.00000    3.00000    0.00000
    6.80000    7.00000    3.00000    0.00000
   10.50000    6.00000    6.00000    1.00000
   15.50000    3.00000    6.00000    1.00000
   16.00000    4.00000    6.00000    2.00000
   18.90000    2.00000    6.00000    2.00000
   23.50000   -1.00000    0.00000    2.00000
   33.60000   -5.00000    0.00000    3.00000
   34.80000   -5.00000    0.00000    3.00000

octave:5> # This is meaningless data, but has two ratio/interval variables
octave:5> # in columns 1 and 2, an ordinal variable in column 3 and the
octave:5> # class in column 4 is a nominal variable.  
octave:5> 
octave:5> x = file_io
x =

    4.50000    8.00000    3.00000    0.00000
    6.80000    7.00000    3.00000    0.00000
   10.50000    6.00000    6.00000    1.00000
   15.50000    3.00000    6.00000    1.00000
   16.00000    4.00000    6.00000    2.00000
   18.90000    2.00000    6.00000    2.00000
   23.50000   -1.00000    0.00000    2.00000
   33.60000   -5.00000    0.00000    3.00000
   34.80000   -5.00000    0.00000    3.00000

octave:6> 
octave:6> # Get the basic statistics
octave:6> 
octave:6> mean (x)
ans =

  18.2333   2.1111   3.3333   1.5556

octave:7> var (x)
ans =

  116.2950   23.6111    7.7500    1.2778

octave:8> std (x)
ans =

  10.7840   4.8591   2.7839   1.1304

octave:9> 
octave:9> # make a new matrix for variables 1 and 4 
octave:9> y = [x(:,1) x(:,4)] 
y =

   4.50000   0.00000
   6.80000   0.00000
  10.50000   1.00000
  15.50000   1.00000
  16.00000   2.00000
  18.90000   2.00000
  23.50000   2.00000
  33.60000   3.00000
  34.80000   3.00000

octave:11> 
octave:11> # plot y    
octave:11> 
octave:11> gplot y with points
octave:12> 
octave:12> # or you can use   
octave:12> plot (y, "@")
octave:12>
octave:13> # Get the covariance matrix  
octave:13> 
octave:13> cov (x)
ans =

   116.2950   -52.1292   -18.4625    11.5917
   -52.1292    23.6111     8.9583    -5.0694
   -18.4625     8.9583     7.7500    -1.3333
    11.5917    -5.0694    -1.3333     1.2778

octave:14> 
octave:14> 
octave:14> # Get the correlation matrix
octave:14> 
octave:14> cor (x)
ans =

   1.00000  -0.99481  -0.61498   0.95091
  -0.99481   1.00000   0.66224  -0.92294
  -0.61498   0.66224   1.00000  -0.42370
   0.95091  -0.92294  -0.42370   1.00000

octave:15> 
octave:15> # This is the Pearson's Correlation Coefficient (Pearson's r)
octave:15> # so it makes sense only for the first two variables.  
octave:15> 
octave:15> 
octave:15> # Calculate some other correlations 
octave:15> spearman ([x(:,3) x(:,4)])

ans =

   1.00000  -0.42230
  -0.42230   1.00000

octave:16> spearman (x)

ans =

   1.00000  -0.97909  -0.50780   0.97040
  -0.97909   1.00000   0.50993  -0.93135
  -0.50780   0.50993   1.00000  -0.42230
   0.97040  -0.93135  -0.42230   1.00000

octave:17> 
octave:17> 
octave:17> kendall ([x(:,3) x(:,4)])

ans =

   1.00000  -0.28645
  -0.28645   1.00000

octave:18> 
octave:18> kendall (x)

ans =

   1.00000  -0.92967  -0.32686   0.91287
  -0.92967   1.00000   0.33150  -0.86410
  -0.32686   0.33150   1.00000  -0.28645
   0.91287  -0.86410  -0.28645   1.00000

octave:19> 
octave:19> # Save the session for a future time
octave:19> 
octave:19> save file_io_sessions
octave:20> exit