[R] Dataframes in PLS package

Bjørn-Helge Mevik b.h.mevik at usit.uio.no
Mon Mar 5 11:18:26 CET 2012


westland <westland at uic.edu> writes:

> Here is the dput(eqn)  and showData for the file 'eqn':
[...]
>> showData(eqn)
>  
> depy.w depy.h depy.d depy.s indx.a indx.i indx.r indx.x
>   63     55      1      0     44  37200      4      0
>    145     52      1      1     33  69300      4      1
>    104     32      0      1     68  56900      3      1
>    109     69      1      1     94  44300      6      1
>    221     61      0      1     72  79800      6      0
>    110     40      1      1     48  17600      5      1
>    194     41      0      0     85  58100      4      0
>    120     76      1      1     19  76700      3      0
>    210     61      0      0     41  37600      1      0 ... etc.

Okay, let me guess: you took the data in the file pls, created a data
frame eqn with two matrices in it, then used write.table() to write
eqn to a file, and then read it back with read.table().

If that is so, the problem you have is that write.table() will separate
the coloumns of the matrices into separate coloumns in the file (it
really has no other choice), and then read.table() will of course read
those in as separate coloumns again.

You have two solutions:

1) Repeat the commands to recreate the eqn data frame as a a data frame
with matrices, after reading it in from file:
   eqn <- data.frame(depy = I(as.matrix(eqn[,1:4])), 
                     indx = I(as.matrix(eqn[,5:8])))

2) Save the data frame in an .RData file with save() instead of as a
text file with write.table().  That will keep the structure of the
variable.


>
>
>
> Initially, I had input a file 'pls' with the script:
>
> dep <- pls[,1:4]
> ind <- pls[,5:8]
> eqn <- data.frame(depy = dep, indx = ind)
> apls <- plsr(depy ~ indx, data=eqn)
>
> .... and this gives me   [7] ERROR:  object 'depy' not found

because you are missing the I(as.matrix()).

-- 
Regards,
Bjørn-Helge Mevik



More information about the R-help mailing list