[R] data structure for plsr

Bjørn-Helge Mevik b.h.mevik at usit.uio.no
Fri Oct 5 13:39:27 CEST 2012

Emma Jones <evjones at ualberta.ca> writes:

> My current data structure consists of a .csv
> file read into R containing 15 columns (a charcoal dilution series going
>>From 100% to 0%) and 1050 rows of absorbance data from 400 nm to 2500 nm at
>  2 nm interval. I think I need to transpose the data such that the specific
> wavelengths become my columns and dilutions are defined in rows,

Yes, you need to transpose the data so a coloumn corresponds to a
variable (response or predictor).

> Should I (and how do I) make my absorbance data into
> individual matrices that read into a data frame with only two columns

It is best to put all predictors (wavelengths) together in one matrix,
yes.  The same for the responses, if you have more than one response

This is untested, so there might be errors:

Assuming that your spectroscopic data is read into a data frame called

## This should create a matrix with the wavelengths as coloumns:
spec <- t(as.matrix(origspec))

I don't know what your response is, so I'm just assuming it is in a
vector called resp.

# This would create a data frame suitable for plsr():
mydata <- data.frame(resp = resp, spec = I(spec))

Then you can analyse like this:

plsr(resp ~ spec, data = mydata, ....)

Bjørn-Helge Mevik

More information about the R-help mailing list