Python is a programming language very similar to C. Python is free, but offers many mathematical and statistical resources that are available from expensive softwares such as Matlab, SAS, and Stata. Because of this, Python is popular with engineers and web developers. Python is also very easy to learn, and there is a very large supportive user network on the www.

StatTools uses Python initially to access complex multivariate statistical routines, but over time accumulated short code segments that may be useful for those who wish to build their own statistical resources.

Python is too big and too complex a subject to explain in this particular web site. For the beginner, https://www.python.org/ is the Python Home Page, from which downloads, tutorials, and other resources can be obtained.

Installing Python

Python was originally designed to run on the DOS. After downloading the installer from https://www.python.org/ and installing Python on the computer, an experienced programmer or engineer will open a DOS box and run Python without any difficulty. For users who are not professional programmers, and who are used to running things on a GUI, a better interface would be useful, and a common interface is Anaconda, accessible from
https://www.anaconda.com/download/.

Anaconda is a large framework which provides at least 4 programming interfaces, and allows downloads of many other supportive facilities. My favourite programming interface is SPYDER, which contains a console very similar to any text editor, and allows any program written to run immediately, producing results in a results panel.

Most Python codes offered from StatTools are therefore developed in the SPYDER client, and instructions on programming offered assumes that the user will be using SPYDER.

Python codes from StatTools

Short snippets of codes are offered as subroutines. Longer codes that perform a complete set of analysis (e.g. codes for logistic regression) are offered as a complete program. Most codes can be copied and pasted into the SPYDER consol and run immediately.

StatTools assumes that the user will have at least beginner's skills in programming, and is either already familiar with Python, or is able and willing to learnit from the resources offered from https://www.python.org/. There is no specific instructions on how to program in Python from StatTools

Output on the console

Directory

By default, R assumes the file is in the same directory as the program. Two commands allows you to find and set the directory

getwd() #returns the current directory

setwd("MyDirectory") # sets the directory to MyDirectory

Read from text files

mx
The content from the file MyInputFile.txt will be read into the matrix ms
The default setting for header is TRUE, meaning that the first row contains the names of the columns
The columns are assumed to be separated by white spaces

mx
The content from the file MyInputFile.txt will be read into the matrix ms
The default setting for header is TRUE, meaning that the first row contains the names of the columns
The columns are separated by s any separator, common ones are "," or ";" or "\t" or " "

mx
the same as mx mx
the same as mx mx
this is used when the delimiter is other then "," or ";" or "\t" or " "

Write to text files

write.table(mydata, "mydata.txt", sep="\t")

write.table(mydata, "mydata.csv", sep=",")

Access excel resources

These must preceed any command using excel files

install.packages("XLConnect")library('XLConnect')

Read from excel files to a variable in R

There are a number of commands and options. For simplicity I use the following

MyInputExcelFile.xlsx is the excel file, the first row contains the names of the columns
if more than 1 sheet exists in the excel file, the sheet accessed can be chosen by the number or the name of the sheet.
MyData is the matrix of data to be used in R.

Save contents of a variable in R to excel files

It takes 4 steps, create a workbook, any number of (create a worksheet, write to the worksheet), and save the workbook