# R Programming/Mathematics

R Programming
* R Basics
** Introduction
** Sample Session
** Manage your workspace
** Settings and Installation
** Documentation
** Control Structures
** Working with functions
** Debugging
** Using C or Fortran
** Utilities
** Estimation utilities
** Packages
* Data Management
** Data types
** Working with data frames
** Importing and exporting data
** Text Processing
** Times and Dates
* Graphics
** Grammar of graphics
* Publication quality output
* Descriptive Statistics
* Mathematics
** Optimization
** Probability Distributions
** Random Number Generation
* Statistical Core Methods
** Maximum Likelihood
** Method of Moments
** Bayesian Methods
** Bootstrap
** Multiple Imputation
** Nonparametric Methods
* Regression Models
** Linear Models
** Quantile Regression
** Binomial Models
** Multinomial Models
** Tobit And Selection Models
** Count Data Models
** Duration Analysis
* Time Series
* Factor Analysis
* Classification
** Ordination
** Clustering
* Network Analysis
* High Performance Computing
** Profiling R code
** Parallel computing with R
* Appendix
** Sources
** Index

## Linear Algebra
### Vectors
#### The inner product

The inner product is also called the dot product or the scalar product. It is the sum of the item-by-item product.

#### The outer product

The outer product is also called the cross product or the vector product. It is a matrix resulting from the product of the elements of the two vectors.

### Matrix Algebra

If you want to create a new matrix, one way is to use the matrix function. You have to enter a vector of data, the number of rows and/or columns and finally you can specify if you want R to read your vector by row or by column (the default option) with byrow. You can also combine vectors using cbind or rbind. The dimension of a matrix can be obtained using the dim function or alternatively nrow and ncol.

### Some special matrix

The identity matrix has ones on the diagonal and zeros outside the diagonal.
* eye (matlab)
* diag(1,nrow=10,ncol=10)
* diag(rep(1,10))
J matrix is full of ones
* ones (matlab)
A matrix full of zeros
* zeros (matlab)
Diagonal matrix
Upper triangular
Lower triangular
Same as upper triangular but using lower.tri instead
* create an Hilbert matrix using hilbert (fUtilities).

### Matrix calculations

* compute a matrix multiplication X%*%Y.
* compute the Kronecker product using %x% or kron (fUtilities).

### Matrix transposition

* Transpose the matrix

### The trace and determinant of a matrix

* compute the trace of a matrix using tr (fUtilities)
* returns the rank of a matrix using rk (fBasics:)

### Matrix inversion

* Invert a matrix using solve or inv (fUtilities). We can also compute the generalized inverse using ginv in the MASS package.

### Eigenvalue, eigenvector and eigenspace

* Eigenvalues and eigenvectors

### Misc

* compute the norm of a matrix using norm (fUtilities).
* check if a matrix is positive definite isPositiveDefinite (fUtilities).
* make a matrix positive definite makePositiveDefinite (fUtilities).
* computes row statistics and column statistics (fUtilities).
* extract the upper and the lower part of a matrix triang and Triang (fUtilities).
* See also the matrix, matlab, matrixcalc, matrixStats packages.

## Analysis
### Logarithm and Exponents

We have the power function 10^3 or 10**3 , the logarithm and the exponential log(2.71), log10(10),exp(1).

### Polynomial equations

To solve $ax^{k}+bx^{k-1}+\cdots +n=0$, where $a,b,\dots ,n$ are given numbers, use the command
So, for example, to calculate the roots of the equation $2x^{2}-5x-3=0$ one would do as follows:
and the solution can be read to be $x_{1}=-0.5\land x_{2}=3$.
See also polynom and multipol packages

### Derivatives
#### Symbolic calculations

R can give the derivative of an expression. You need to convert your function as an expression using the expression function. Otherwise you get an error message.
Here are some examples :

#### Numerical approximation

* numDeriv package

### Integration

R can perform one dimensional integration. For example we can integrate over the density of the normal distribution between $-\infty$ and $+\infty$
see the adapt package for multivariate integration.
* See also integrate.gh in the ecoreg package.

## Probability

* The number of combination of length k within n numbers : $C_{n}^{k}$
* Union and intersection

## Arithmetics
### The factorial function

factorial returns the factorial of an integer. This can also be computed using the prod (product) applied to the vector of integers between 1 and the number of interest.
Note that by convention $0!=1$. factorial returns 1 in 0. This is not the case with the prod functions.
Factorial numbers can be very large and cannot be computed for high values.

### The modulo function and euclidian division

* Modulo and integer division (i.e. euclidean division)
Note: R is affected by the problem with non integer numbers and euclidian divisions.

## Geometry

* pi the constant
* cos, sin, tan the trigonometric functions.

## Symbolic calculus

rSymPy (rsympy) provides sympy (link) functions in R.
If you want to do more symbolic calculus, see Maxima, SAGE, Mathematica

## See also

The following command gives help on special mathematical functions related to the beta and gamma functions.

## References

* ↑ Maxima is open source http://maxima.sourceforge.net/
* ↑ SAGE is an open source package which includes R and Maxima : http://www.sagemath.org/
* ↑ Mathematica is not open source http://www.wolfram.com/products/mathematica/index.html