How to run a program in Splus


In some of these methods, you need to create a file in some of your subdirectories with some editor. Suppose that you want to work in your subdirectory "/home/arcones/MySwork" and you want to create a file named "myprogram". First open an editor, type the commands and save your file in the correct directory. A possible program is:

************************
n_20
N_100
qu_c(1,2,3)
b_3*N
qua_c(1:b)
qua_matrix(qua,ncol=3,byrow=T)
qua.mean_qua
names(qu)_c("Q1","Me","Q3")
for(i in 1:N) 
{
x1_rnorm(n)
qua[i,1]_quantile(x1,0.25)
qua[i,2]_quantile(x1,0.5)
qua[i,3]_quantile(x1,0.75)
}
qu[1]_mean(qua[,1])
qu[2]_mean(qua[,2])
qu[3]_mean(qua[,3])
return(qu)
************************
You do not need to close your editor. You only need to save the program to being able to run it. You can edit your program while you are running Splus. In this way, you do corrections to a program and run the program again and again.

1st way

Go to your directory "/home/arcones/MySwork". Create a file named 'myprogram" with an editor. Then, without opening Splus, type in your console:
Splus BATCH myprogram outfile
myprogram is the name of the program. outfile is the name where the outcome of the program comes out. You need to be in the directory where your file "myprogram" is. Using an editor (or the Linux commands cat, more or less) you can see the contents of the file name "outfile". Instead of "myprogram" and "outfile" you can use other names.

2nd way

First create a file named 'myprogram" in one of your Linux directories. Open Splus and type:

 
source("/home/arcones/MySwork/myprogram")
here "/home/arcones/MySwork/" is the directory you are using and "myprogram" is the name of the program you want to run.

In this way you will run the program. The return of the program is limited to what you type as

> return(variable)
You will get the value in the object "variable". Instead of variable you can use another name.


3rd way

In Splus create a function with the commands and withour arguments, you want to run:
> myprogram_function(n,N){
qu_c(1,2,3)
b_3*N
qua_c(1:b)
qua_matrix(qua,ncol=3,byrow=T)
qua.mean_qua
names(qu)_c("Q1","Me","Q3")
for(i in 1:N) 
{
x1_rnorm(n)
qua[i,1]_quantile(x1,0.25)
qua[i,2]_quantile(x1,0.5)
qua[i,3]_quantile(x1,0.75)
}
qu[1]_mean(qua[,1])
qu[2]_mean(qua[,2])
qu[3]_mean(qua[,3])
myprogram_qu
}
Then, run the program doing:
> myprogram(20,100)
         Q1        Me        Q3
 -0.1877989 0.1430636 0.7241218

My favorite way

First create a file named "myprogram" in one of your Linux directories. Then, open Splus and type:

 
> run_function(){source("/home/arcones/MySwork/myprogram")}
> run()
         Q1          Me        Q3
 -0.6223571 0.007234289 0.6318713
You can use another name instead of "run". To run the program again, you just need to type run(). You do not to type "run_function(){source("/home/arcones/MySwork/myprogram")}". The function "run" will be stored in the memory. To type run() is easier to type than source("/home/arcones/MySwork/myprogram").

Tips

Make a program by pieces. You can make parts which Splus will not compile by starting a line with #. In this way, you run only part of a program. You also can insert comments.
# This program does  N simulations of samples from a normal 
# distribution with sample size n. We find the average of Q1, Me and Q3 
# for the N simulations. These averages constitute the outcome of the
# program. 
#
# Here, I give the values of 
n_20
N_1000
qu_c(1,2,3)
b_3*N
qua_c(1:b)
qua_matrix(qua,ncol=3,byrow=T)
qua.mean_qua
names(qu)_c("Q1","Me","Q3")
# This is a loop
for(i in 1:N) 
{
x1_rnorm(n)
qua[i,1]_quantile(x1,0.25)
qua[i,2]_quantile(x1,0.5)
qua[i,3]_quantile(x1,0.75)
}
qu[1]_mean(qua[,1])
qu[2]_mean(qua[,2])
qu[3]_mean(qua[,3])
# qu contains the average of N quantiles from a sample
return(qu)