[R] qr.solve and lm

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Tue May 15 15:24:55 CEST 2007


Vladimir Eremeev wrote:
> Dear R experts,
>
> I have a Matlab code which I am translating to R in order to examine and
> enhance it.
> First of all, I need to reproduce in R the results which were already
> obtained in Matlab (to make sure that everything is correct).
>
> There are some matrix manipulations and '\' operation among them in the
> code.
>
> I have the following data frame
>
>   
>> ABS.df
>>     
>            Pro        syn             unk                 Chl                 
> Y                  Nh            V1
> 1  0.056524968 0.04387755 -0.073925372 0.026477146 0.083527021 0.0031950622 
> 0.02164793
> 2  0.066456284 0.05992579 -0.094609497 0.031772575 0.054881164 0.0022078221 
> 0.01358594
> 3  0.036383887 0.04601113 -0.061213302 0.021181717 0.026713530 0.0011716424 
> 0.00824816
> 4  0.020419528 0.03803340 -0.044517584 0.013935340 0.019204991 0.0008763467 
> 0.00577604
> 5  0.010209764 0.02968460 -0.030418334 0.009476031 0.013806924 0.0006554761 
> 0.00376991
> 6  0.006033043 0.02666976 -0.025735131 0.006967670 0.009778344 0.0004838437 
> 0.00441753
> 7  0.004733618 0.01168831 -0.009732966 0.006688963 0.002351775 0.0001380701 
> 0.00403229
> 8  0.000000000 0.00000000  0.000000000 0.000000000 0.000000000 0.0000000000
> -0.00060456
> 9  0.000000000 0.00000000  0.000000000 0.000000000 0.000000000 0.0000000000
> -0.00000003
> 11 0.241505077 0.45843930 -0.611308847 0.000000000 0.000000000 0.0584138174 
> 0.07397018
> 21 0.226065730 0.41703452 -0.558270870 0.000000000 0.000000000 0.0516895121 
> 0.07460447
> 31 0.211956969 0.37139373 -0.495174662 0.000000000 0.000000000 0.0426347324 
> 0.07189945
> 41 0.206558807 0.35022863 -0.466337208 0.000000000 0.000000000 0.0392775042 
> 0.06943217
> 51 0.197535970 0.33100673 -0.441656561 0.000000000 0.000000000 0.0363102112 
> 0.06912282
> 61 0.186798904 0.31656506 -0.416287992 0.000000000 0.000000000 0.0335613531 
> 0.06595377
> 71 0.156005203 0.24868275 -0.329660100 0.000000000 0.000000000 0.0250152915 
> 0.06399879
> 81 0.153751864 0.23900952 -0.324902567 0.000000000 0.000000000 0.0232553862 
> 0.05981515
> 91 0.144414605 0.22782217 -0.297243170 0.000000000 0.000000000 0.0209515025 
> 0.05981442
>
>   
>> qr.solve(ABS.df[,1:6],ABS.df[,7])
>>     
>        Pro        syn        unk        Chl          Y         Nh 
>  0.3877544  0.4282277  0.2221960 -0.8668715  0.2821082 -1.3696411 
>
> This reproduces the Matlab's numbers 
> However, I used to lm, its syntax seems to me more clear.
>
> ?lm says that it uses QR decomposition to fit the model.
> Trying it:
>
>   
>>  coef(lm(V1~Pro+syn+unk+Chl+Y+Nh,data=ABS.df))
>>     
>  (Intercept)          Pro          syn          unk          Chl           
> Y           Nh 
>  0.001640184  0.417253116  0.351472810  0.196977369 -0.899729874 
> 0.265585292 -1.181526491 
>
> Numbers differ.
> Obviously, I don't understand something. 
> Please, could you clarify, what?
> Thank you.
>
>   
Your data came across garbled and I'm not in the mood to fix it for you...

However, the presence of an extra coefficient in the latter case looks
like the giveaway: You need to generate the (Intercept), most likely you
need  qr.solve(cbind(1,ABS.df[,1:6]), ABS.df[,7]), but (see above) I
didn't actually test that.

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list