On Tue, 28 Apr 2009, Sven Schreiber wrote:
I have suggestions about creating series from matrices in order
to plot the columns of a calculated matrix.
First of all, I think it would be useful if matrices columns (or
rows) could be directly used in plots without any conversion to
series, though I might be convinced otherwise if the conversion
to series didn't involve the problems that I describe below.
That would not be very easy.
Second, a GUI-way of converting matrices to series would be
nice, for example in the session/symbol view right-click a
matrix icon and have a context menu entry "create series from
columns" or some such. So far I'm only aware of the script
command, see guide chapter 12.9.
I tend to think of matrices as inherently "scripty" things. Such
a dialog would have to be fairly complicated, with slots for names
for all the columns and apparatus for setting offsets or
truncation if the number of rows doesn't match the sample. But it
could be done I guess.
Then, here are some problems with the various workarounds that I
tried:
1) first method: after calculating the relevant matrix do
'nulldata <appropriate sample size> --preserve'
and convert the matrix columns to series for plotting.
problems with that:
* the stuff in the old dataset isn't available anymore which is inconvenient
* I guess session handling with several workfiles is also problematic
quirks encountered along the way:
* why isn't the nulldata thing possible via GUI? "New dataset" is a
greyed-out menu item when another dataset is opened, but I see no real
reason for that.
That item becomes active if you do "Clear dataset" first.
* it seems impossible to load a matrix from file via GUI? (equivalent
to
'mread', while GUI 'mwrite'-equivalent exists)
2) next method: convert the matrices to series (with script commands) in
the same workfile/dataset, after setting the sample size appropriately
problems here:
* after (each) plotting, the original sample has to be restored manually
-- it's easy to automate if that was the full dataset, but not for some
specifically chosen sample (right?)
Hmm, for this sort of thing it _might_ be nice to be able to
record the current sample in a "mask" series -- something like
smpl <some complicated stuff>
series mask = $smplmask
smpl <whatever>
<do stuff>
# get the saved sample back
smpl mask --restrict --replace
But then you can in effect do this already by defining a suitable
dummy.
quirks encountered:
* I find the paragraph in guide ch. 12.9 a little confusing (and after I
understand everything myself, I volunteer to rewrite it)
Sounds good!
* I guess I understand the spirit of the restriction that the vector
length has to match the sample, but strictly speaking it's not
necessary; the dataset or sample dimension must be at *least* as big as
the number of matrix elements, but why exactly equal? Without the
stricter restriction, this workaround no. 2 would be easier, FWIW.
Would we fill in values from the start of the sample and pad with
NAs at the end? It seems a bit arbitrary but could be done.
3) next method: same as in (2), but encapsulate that in a function
which
does the conversion and plotting; the effect is that the sample of the
dataset isn't affected because it's outside the function scope
problem:
* after the function is done, the series are also gone (though it may
work to pass them back somehow using another dirty workaround, haven't
tried that)
No dirty workaround required, just return a list.
<script>
function mconv (const matrix *m)
scalar T = rows(m)
smpl 1 T
scalar k = cols(m)
list mlist
loop i = 1..k
series m$i = m[,i]
mlist += m$i
endloop
return list mlist
end function
open data4-1
matrix m = mnormal(10,2)
list L = mconv(&m)
print L -o
</script>
The bottom line still is that I would welcome direct plotting of
matrix columns/rows in scatterplots or against an index/time
variable. Again, think of right-clicking a matrix icon and
choose a context menu entry for example.
Well, I can see that would be nice. Maybe it's not so hard after
all.
Allin.