[R] automated polynomial regression

Luc Villandre villandl at dms.umontreal.ca
Thu May 14 19:02:21 CEST 2009


Dieter Wirz wrote:
> Dear all -
> We perform some measurements with a machine that needs to be
> recalibrated. The best calibration we get with polynomial regression.
> The data might look like follows:
>
>   
>> true_y <- c(1:50)*.8
>> # the real values
>> m_y <- c((1:21)*1.1, 21.1, 22.2, 23.3 ,c(25:50)*.9)/0.3-5.2
>> # the measured data
>> x <- c(1:50)
>> # and the x-axes
>>
>> # Now I do the following:
>>
>> m_y_2 <- m_y^2
>> m_y_3 <- m_y^3
>> mylm <- lm(true_y ~ m_y + m_y_2 + m_y_3) ; mylm
>>     
>
> Call:
> lm(formula = true_y ~ m_y + m_y_2 + m_y_3)
>
> Coefficients:
> (Intercept)          m_y        m_y_2        m_y_3
>   1.646e+00    1.252e-01    2.340e-03   -9.638e-06
>
> Now I can get the real result with
>   
>> calibration <- 1.646e+00 + 1.252e-01*m_y + 2.340e-03*m_y_2 - 9.638e-06* m_y_3
>>     
> But I have to put the values "by hand" into the polynom.
>
> Isn't there an easier, more direct way, or at least , is there a way
> to access the coefficients of lm() for further calculations?
>
> Thanks
> Dieter
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>   
Hi Dieter,

The lm() function will create an object with members that can easily be 
accessed. Just save the result of your regression in a variable and 
output the needed quantity. For example,

> mylm <- lm(true_y ~ m_y + m_y_2 + m_y_3) ;
>   
## Now, what I believe you're looking for ;

mylm$coefficients ;


Cheers,

-- 
*Luc Villandré*
/Biostatistician
McGill University Health Center -
Montreal Children's Hospital Research Institute/




More information about the R-help mailing list