dataset minimally works with case and var names. Examples included. Exports done.
[CommonLispStat.git] / data-clos.lisp
blob0e589143af1626894a0fcaf5087d953cd6839c1f
1 ;;; -*- mode: lisp -*-
3 ;;; File: data-clos.lisp
4 ;;; Author: AJ Rossini <blindglobe@gmail.com>
5 ;;; Copyright: (c)2008, AJ Rossini. BSD, LLGPL, or GPLv2, depending
6 ;;; on how it arrives.
7 ;;; Purpose: data package for lispstat
8 ;;; Time-stamp: <2008-03-12 17:18:42 user>
9 ;;; Creation: <2008-03-12 17:18:42 user>
11 ;;; What is this talk of 'release'? Klingons do not make software
12 ;;; 'releases'. Our software 'escapes', leaving a bloody trail of
13 ;;; designers and quality assurance people in its wake.
15 ;;; This organization and structure is new to the 21st Century
16 ;;; version.
18 ;;; data-clos.lisp
19 ;;;
20 ;;; redoing regression in a CLOS based framework.
21 ;;; See regression.lsp for basis of work.
23 (in-package :cl-user)
25 (defpackage :lisp-stat-data-clos
26 (:use :common-lisp
27 :clem ;;?? or :matlisp , or :...?
29 (:export dataset varNames caseNames))
31 (in-package :lisp-stat-data-clos)
34 ;; Need to figure out typed vectors. We then map a series of typed
35 ;; vectors over to tables where columns are equal typed. In a sense,
36 ;; this is a relation (1-1) of equal-typed arrays. For the most part,
37 ;; this ends up making the R data.frame into a relational building
38 ;; block (considering 1-1 mappings using row ID as a relation).
41 (defclass table ())
43 (defclass relation ())
45 (defclass dataset ()
46 ((store :initform nil
47 :initarg :storage
48 :accessor dataset)
49 (documentation-string :initform nil
50 :initarg :doc
51 :accessor doc-string)
52 (case-labels :initform nil
53 :initarg :case-labels
54 :accessor case-labels)
55 (var-labels :initform nil
56 :initarg :var-labels
57 :accessor var-labels))
58 (:documentation "Standard Cases by Variables Dataset."))
60 ;; Need to set up dataset as a table or a relation. One way or
61 ;; another it should all work out. TODO: How do we do multiple
62 ;; inheritance or composite structures?
64 (defvar my-ds-1 nil
65 "test ds for experiment.")
66 (setf my-ds-1 (make-instance 'dataset))
67 my-ds-1
69 (defvar my-ds-2 nil
70 "test ds for experiment.")
71 (setf my-ds-2 (make-instance 'dataset
72 :storage #2((1 2 3 4 5) (10 20 30 40 50))
73 :doc "This is an interesting dataset"
74 :case-labels (list "a" "b" "c" "d" "e")
75 :var-labels (list "x" "y")))
76 my-ds-2
78 (slot-value my-ds-2 'store)
79 (dataset my-ds-2)
80 (eq (dataset my-ds-2) (slot-value my-ds-2 'store))
82 (doc-string my-ds-2)
83 (case-labels my-ds-2)
84 (var-labels my-ds-2)
85 ;; need to ensure that for things like the following, that we protect
86 ;; this a bit more so that the results are not going to to be wrong.
87 ;; That would be a bit nasty if the dataset becomes inconsistent.
88 (setf (var-labels my-ds-2) (list "a" "b"))
93 (defun print-structure-table (ds)
94 "example of what we want the methods to look like. Should be sort
95 of like a spreadsheet if the storage is a table."
96 (print-as-row (var-labels ds))
97 (let ((j -1))
98 (dolist (i (case-labels ds))
99 (princ "%i %v" i (row-extract (dataset ds) (incr j))))))
101 (defun print-structure-relational (ds)
102 "example of what we want the methods to look like. Should be sort
103 of like a graph of spreadsheets if the storage is a relational
104 structure."
105 (dolist (k (relations ds))
106 (print-as-row (var-labels ds))
107 (let ((j -1))
108 (dolist (i (case-labels ds))
109 (princ "%i %v" i (row-extract (dataset ds) (incr j))))))
114 (defgeneric extract (dataform what into-form))
116 (defmethod extract ((ds dataset) what into-form)
117 (reshape (get ds what) into-form))
121 (defclass data-format())
124 (defun transpose (x)
125 "map NxM to MxN.")
127 (defun reorder-by-rank (x order &key (by-row t))
128 " .")
130 (defun reorder-by-permutation (x perm &key (by-row t))
131 " .")
134 ;;; Variable-name handling for Tables. Needs error checking.
135 (defun varNames (ds)
136 (var-labels ds))
138 (defun set-varNames (ds vN)
139 (if (= (length (var-labels ds))
140 (length vN))
141 (setf (var-labels ds) vN)
142 (error "wrong size.")))
144 (defsetf varNames set-varNames)
146 (varNames my-ds-2)
147 (setf (varNames my-ds-2) (list "a" "b"))
148 (varNames my-ds-2)
151 ;;; Case-name handling for Tables. Needs error checking.
152 (defun caseNames (ds)
153 (case-labels ds))
155 (defun set-caseNames (ds vN)
156 (if (= (length (case-labels ds))
157 (length vN))
158 (setf (case-labels ds) vN)
159 (error "wrong size.")))
161 (defsetf caseNames set-caseNames)
163 (caseNames my-ds-2)
164 (setf (caseNames my-ds-2) (list "a" "b" "c" 4 5))
165 (caseNames my-ds-2)