File: ExportRMultiple.htd

package info (click to toggle)
cain 1.10+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 29,856 kB
  • sloc: cpp: 49,612; python: 14,988; xml: 11,654; ansic: 3,644; makefile: 133; sh: 2
file content (108 lines) | stat: -rw-r--r-- 3,237 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<html>
<head>
<title>Multiple Trajectories</title>
</head>
<body>
<h1>Multiple Trajectories</h1>

<p>
Open the file <tt>examples/cain/WilkinsonSMfSB/ch06-lv.xml</tt>,
which models the Lotka-Volterra system.
In the method editor, change the recording time to 50 and set the
number of frames to 501 so that we record the state every 0.1 time
units. In the launcher panel, set the number of trajectories to 5 and
then run the simulation 
by clicking the launch button <img src="launch.png">.
Next click the export button <img src="filesave.png">&nbsp;
in the simulation output panel.
Export the species trajectories and use &quot;lv&quot; for 
the base name in the file dialog.
</p>


<p>
Launch R and set the working directory to where you have saved the
data file.  Read the CSV (Comma Separated Values) file and have a look
at the first few lines.
</p>

<pre>
&gt; setwd('Development/cain')
&gt; lv.df &lt;- read.csv('lv.csv')
&gt; head(lv.df)
  Time Prey_1 Prey_2 Prey_3 Prey_4 Prey_5 Predator_1 Predator_2 Predator_3 Predator_4
1  0.0     50     50     50     50     50        100        100        100        100
2  0.1     48     55     55     52     53         99         97         89         92
3  0.2     47     58     56     56     55         99         95         80         88
4  0.3     51     66     58     57     63         95         95         72         82
5  0.4     52     67     63     72     71         94         94         68         80
6  0.5     54     71     67     70     73         88         91         68         86
  Predator_5
1        100
2         95
3         96
4         93
5         90
6         87
</pre>

<p>
To make a time series data structure, we drop the first column.
However, we use the times in the first column to define the starting
time and the time increment.
By default, plotting the time series data will yield separate plots
for each species and each trajectory.
</p>

<pre>
&gt; lv.ts &lt;- ts(lv.df[,-1], start=lv.df[1,1], deltat=lv.df[2,1]-lv.df[1,1])
&gt; plot(lv.ts)
</pre>


<p align="center">
<!--Save as 8x6. Export to PNG and then resize to 4x3.-->
<img src="ExportR/Multiple/lvMultiple.png">
</p>

<p>
Next we plot all of the prey trajectories together. The prey
trajectories are column 1 through 5 in the time series data structure.
Instead of using this fact, we show a more general method of selecting
the appropriate columns below. We use the <tt>grep()</tt> function,
which stands for Get Regular ExPression, to select the columns by
matching the column names.
</p>

<pre>
&gt; cols &lt;- grep('Prey', colnames(lv.ts))
&gt; plot(lv.ts[,cols], plot.type='single', ylab='Prey')
</pre>

<p align="center">
<img src="ExportR/Multiple/lvPrey.png">
</p>

<p>
We can also plot all of the prey trajectories and all of the predator
trajectories in a split window.
</p>

<pre>
&gt; split.screen(c(2,1))
[1] 1 2
&gt; screen(1)
&gt; cols &lt;- grep('Prey', colnames(lv.ts))
&gt; plot(lv.ts[,cols], plot.type='single', ylab='Prey')
&gt; screen(2)
&gt; cols &lt;- grep('Predator', colnames(lv.ts))
&gt; plot(lv.ts[,cols], plot.type='single', ylab='Predator')
&gt; close.screen(all.screens=T)
</pre>

<p align="center">
<img src="ExportR/Multiple/lvSplit.png">
</p>

</body>
</html>