[R] How to formulate quadratic function with interaction terms for the PLS fitting model?

David Winsemius dwinsemius at comcast.net
Sun Jul 16 16:36:20 CEST 2017


> On Jul 13, 2017, at 7:43 AM, Bert Gunter <bgunter.4567 at gmail.com> wrote:
> 
> Below.
> 
> -- Bert
> Bert Gunter
> 
> 
> 
> On Thu, Jul 13, 2017 at 3:07 AM, Luigi Biagini <luigi.biagini at gmail.com> wrote:
>> I have two ideas about it.
>> 
>> 1-
>> i) Entering variables in quadratic form is done with the command I
>> (variable ^ 2) -
>> plsr (octane ~ NIR + I (nir ^ 2), ncomp = 10, data = gasTrain, validation =
>> "LOO"
>> You could also use a new variable NIR_sq <- (NIR) ^ 2
>> 
>> ii) To insert a square variable, use syntax I (x ^ 2) - it is very
>> important to insert I before the parentheses.
> 
> True, but better I believe: see ?poly.
> e.g. poly(cbind(x1,x2,x3), degree = 2, raw = TRUE) is a full quadratic
> polynomial in x1,x2,x3 .
> 

Is there any real difference between 

octane ~ NIR * I(NIR^2)
octane ~ NIR * poly(NIR, degree=2, raw=TRUE)

?
(I though that adding raw = TRUE prevented the beneficial process of centering the second degree terms.)
__ 
David
> 
>> 
>> iii) If you want to make the interaction between x and x ^ 2 use the
>> command ":" -> x: I(x ^ 2)
>> 
>> iv) For multiple interactions between x and x ^ 2 use the command "*" -> x
>> *I (x ^ 2)
>> 
>> i) plsr (octane ~ NIR + NIR_sq, ncomp = 10, data = gasTrain, validation =
>> "LOO") I (x ^ 2)
>> ii)p lsr (octane ~ NIR + I(NIR^2), ncomp = 10, data = gasTrain, validation
>> = "LOO") I (x ^ 2)
>> iii)p lsr (octane ~ NIR : I(NIR^2), ncomp = 10, data = gasTrain, validation
>> = "LOO") I (x ^ 2)
>> iv)p lsr (octane ~ NIR * I(NIR^2), ncomp = 10, data = gasTrain, validation
>> = "LOO") I (x ^ 2)
>> 
>> 2 - For your regression, did you plan to use MARS instead of PLS?
>> 
>> 
>> 
>> 
>> Dear all,
>>> I am using the pls package of R to perform partial least square on a set of
>>> multivariate data.  Instead of fitting a linear model, I want to fit my
>>> data with a quadratic function with interaction terms.  But I am not sure
>>> how.  I will use an example to illustrate my problem:
>>> Following the example in the PLS manual:
>>> ## Read data
>>> data(gasoline)
>>> gasTrain <- gasoline[1:50,]
>>> ## Perform PLS
>>> gas1 <- plsr(octane ~ NIR, ncomp = 10, data = gasTrain, validation = "LOO")
>>> where octane ~ NIR is the model that this example is fitting with.
>>> NIR is a collective of variables, i.e. NIR spectra consists of 401 diffuse
>>> reflectance measurements from 900 to 1700 nm.
>>> Instead of fitting with octane[i] = a[0] * NIR[0,i] + a[1] * NIR[1,i] + ...
>>> I want to fit the data with:
>>> octane[i] = a[0] * NIR[0,i] + a[1] * NIR[1,i] + ... +
>>> b[0]*NIR[0,i]*NIR[0,i] + b[1] * NIR[0,i]*NIR[1,i] + ...
>>> i.e. quadratic with interaction terms.
>>> But I don't know how to formulate this.
>>> May I have some help please?
>>> Thanks,
>>> Kelvin
>> 
>>        [[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list