29 may 2012

Exporting from Win ISI / Importing into R

Chemometric software´s  have the option to export a matrix to a TXT file (in this case a constituents matrix), in a way we can import it easily into R, to work with. It is the first step to go into the R world.
I use in this case Win ISI Software, but sure you can do it with othersoftware´s  as well.
See the video:
Exporting from Win ISI / Importing into R

Mahalanobis distance with "R" (Exercice)

I have developed this exercise with Excel in another post for the same calculations , I am going to develop  it this time with  "R".

These are data of lead concentration in fish

     Age Length Weight   mg/Kg

1    28    31   130.0    68.12
2    24    28   143.0   127.89
3    28    20   136.0    89.03
4    32    34   130.5    78.28
5    22    15   125.0   134.08
6    26    37   147.5   135.31
7    24    19   135.0   130.48
8    28    22   125.0    86.48
9    24    26   127.0   129.47
10   30    21   139.0    82.43
11   22    20   121.5   127.41
12   30    38   150.5    71.21
13   24    17   120.0   132.06
14   26    20   125.0    90.85

We import the data into R.

 x<-read.table("C:\\lead_fish.txt",header=TRUE)

We are going to apply the Mahalanobis Distance formula:

D^2 = (x - μ)' Σ^-1 (x - μ)

We calculate μ (mean) with:

mean<-colMeans(x)
   Age     Length     Weight     mg/Kg
 26.28571  24.85714 132.50000 105.93571

We calculate Σ (covariance matrix (Sx)) with:
Sx<-cov(x)
> Sx
         Age       Length     Weight   mg/kg
Age
    9.758242   12.81319  12.07692 -72.15407
Length  12.813187  56.90110  49.11538 -70.62066
Weight  12.076923  49.11538  92.80769 -46.06962
mg/Kg  -72.154066 -70.62066 -46.06962 714.00118

The default value for the Mahalanobis function is inverted=FALSE, so the function will calculate the inverse of Sx. If we calculated appart remember to change to TRUE.
See R help:


O.K. Let´s go:
>D2<-mahalanobis(x,mean,Sx)
> D2
 [1] 5.571677 2.863499 2.686127 7.766153 2.379621 6.366793 2.135347 1.538248
 [9] 2.018812 5.143830 3.082734 5.470313 3.158651 1.818195

These are the values in the Diagonal Matrix we saw with the calculations in Excel.






 

25 may 2012

Monitor: Adding "RER" and "RPD" statistics

I continue developing the Monitor function in R. The idea is to get statistics which help me to understand the performance of my model.
Of course the validation set must be free of outliers (X or Y).
I add this time two new statistics: RER and RPD.
These statistics must be treated with caution, because depends of the range, standard deviation and number of samples for the validation set.
See the new video for the calculation of these statistics in "R":
 Monitor: Adding "RER" and "RPD" statistics

23 may 2012

First Derivative (Prof. Gilbert Straw Lecture)

We talked in this blog quite often about the first derivative as one of the main math treatments to apply to the spectra. This video is good way to understand the calculus behind this math treatment.

Ferris Bueller's Day Off

It is always good to see Gilbert Strang (MIT) lectures. At one point of the lecture, Prof. Strang talks about one film, and justifies the idea of the protagonist.
This is the film Prof. Gilbert Strang talks about on his lecture,.., I remember the film but not the title so I have to use Google to find it.
Sadly correct calculus was not applied by the manufacturer and a disaster happens.