Share My Creation [B4X] Multivariate Analysis, Matrices, Complex Numbers, One Thing Leads to Another

I was interested in exploring Multivariate Analysis in B4X. This topic includes:
covariance, correlations, multivariate analysis of variance (MANOVA),
multiple linear regression (MLR), principal component analysis (PCA),
factor analysis with factor rotation (VARIMAX), multidimensional scaling (MDS)
cluster analysis, logistic regression, and more.

Very soon I realized I needed a flexible double precision matrix class. After I developed
one I was comfortable with (dMatrix), I found that for some multivariate procedures, complex numbers
make things much more elegant and concise. So I started on developing a class (cMatrix) for that.

This task became trivial when I realized that a matrix of complex numbers can be thought of as a pair of matched
and synchronized matrices of double precision numbers. Most of the work could be done by dMatrix.

So here I present the two classes, as well as a series of examples, including two MLR projects and one
Factor Analysis project.

Preparing for this submission, I found it awkward to describe things without showing both the code
and the results in the same window, so I also developed a class dedicated to doing that: WiLDemo.

One thing leads to another. To choose between examples, I used B4XListTemplate in a separate class (WiLOptions)
This class takes an array of items and displays it across platforms in a simplified way.

When I started to implement the statistics examples, I needed a class to compute cumulative probability density values.
For this class (WiLStats) I was able use part of one of my other projects, NCrunchR:

https://www.b4x.com/android/forum/threads/number-cruncher-and-statistical-package-integrator.102566/
[The present project is part of a remake of NCrunchR I am working on that will be based on B4XPages and B4XTable]

There is a library/class wrapping of JAMA authored by Johan Schoeman. It is very good but doesn't meet my needs in this case.
https://www.b4x.com/android/forum/threads/jama-a-java-matrix-package-b4a-wrapper-in-post-3.59192/

I needed a B4X matrix class that would do all the standard things that R, MATLAB, Python and others can do with matrices.
1. It should have a simple syntax
2. It should be able to display itself
3. It should be chainable: Matrix.method1.method2.method3 etc.
4. It should have inverse, single value decomposition and eigen vectors/values implemented in native B4X
5. It should make efficient use of B4X facilities
6. It should be multi-platform
7. It should be independent of other packages such as R
8. It should have built-in error reporting with traceable names
9. It should avoid cryptic indexing for applying methods to rows, columns, elements
10.It should be able to accomodate missing values, and handle them in functions like sum and find

Instances of dMatrix have four public properties and more than 60 public methods:
id = a string version of the variable name (used to identity matrices involved in erroneous operations)
nr = number of rows
nc = number of columns
x = the 2 dimensional array of double precision numbers

Instances of cMatrix have five public properties: id, nr, nc, dMx_R, dMx_I
where dMx_R (Real part) and dMx_I (Imaginary part) are instances of dMatrix

The attached project is based on B4XPages. It works in B4J and B4A, but I haven't tested it in B4i.
Other than defining fx, and a monospaced font, there is no platform specific code.

B4XPages makes it simple to show different reports (without unnecessarily re-computing them)
and different instances of a report class. Composing statistical reports is also greatly simplified
by using BBCodeView in report classes.

dMatrix and cMatrix have no dependencies and work with or without B4XPages on B4J and B4A and probably on B4i (not tested).

The code is here for all to use as desired, I'll use it for my own purposes, and I don't plan to commercialize it.
I have tested it extensively, but I have to make a disclaimer. If you use this code, then you are responsible
for it's correctness and the appropriateness of it's application. If you publish it, include a link to this post.

If you use statistics for scholarly or professional work you must use SPSS, SAS, MATLAB, R, or other established package.
If you like to experiment or learn about the inner workings works of statistics, you may be interested in this.
If you find errors, omissions, or lack of clarity, I would like to fix them. So let me know.

The classic work on this was done many years ago, including the 'pivotal' work of Carl Friedrich Gauss (1777-1855).
I want to acknowledge the influence of the programming language APL designed in the 1960s by Kenneth E. Iverson.
APL is an array-based language. In 1972 it was implemented successfully on an 8K CPU with 64k of ROM - the MCM700.

My fondness for APL, while not shared by many, inspired me to develop dMatrix for B4X, which is now my all-time favourite.

Note: Size constraints required splitting up the project. Unzip the following 3 zip files into a single folder, and all will be Ok.
 

Attachments

  • MatricesB4A.zip
    244.2 KB · Views: 298
  • MatricesB4J.zip
    150 KB · Views: 305
  • Shared Files.zip
    107.7 KB · Views: 299

William Lancee

Well-Known Member
Licensed User
Longtime User
I have packaged the 8 classes into three libraries.

\WiLMatrices\
cMatrix.bas
decomposition.bas
dMatrix.bas
manifest.txt

\WiLStatistics\
2.bal
2.bjl
manifest.txt
WiLMultRegression.bas
WiLOptions.bas
WiLPCAFactor.bas
WiLStats.bas

\WiLDemo\
1.bal
1.bjl
manifest.txt
WiLDemo.bas

Copy the 3 .b4xlib files to AdditionalLibraries\B4X\

Unzip the 3 .zip files into one folder to try the library version of the project.
 

Attachments

  • WiLDemo.b4xlib
    6.7 KB · Views: 267
  • WiLMatrices.b4xlib
    16.4 KB · Views: 275
  • WiLStatistics.b4xlib
    13.9 KB · Views: 272
  • Shared Files.zip
    107.7 KB · Views: 293
  • MatricesB4A.zip
    213.2 KB · Views: 274
  • MatricesB4J.zip
    118.9 KB · Views: 281
Last edited:
Hi William,

Sounds interesting. I am not a skilled programmer so do not understand all you are posting.

Suppose i want to do multiple linear regression on some data with multiple independant variables and one dependant variable, can this be easily done with your code library?

And can you show how? Thank you!
 

William Lancee

Well-Known Member
Licensed User
Longtime User
The short answer is Yes. However, "easily" depends on your programming experience.
The library is a set of tools and not an end-user statistical package.

If...
1. You are familiar with B4X libraries and B4XPages
2. You know how to use instances of classes
3. You have your data in the expected format
4. You understand the limits of MLR and can interpret the results
5. You don't need to justify the Statistical Package used to a Thesis Committee or an Editorial Board

Then Yes, easily, and I can guide you through the steps.
 
Last edited:

William Lancee

Well-Known Member
Licensed User
Longtime User
Start by downloading and trying out the demo in post #1. There are two examples of MLR, two clicks away.
 
Top