[R] Problems with data structure when using plsr() from package pls

CG Pettersson cg.pettersson at lantmannen.com
Mon Jan 18 11:25:14 CET 2016


Again: thanks a lot for all your suggestions, the problem is now solved.

This combination of calls finally did the trick:

> n96 <- as.matrix(n96)
> frame2 <- data.frame(gushVM, n96=I(n96))
> str(frame2)
'data.frame':   15 obs. of  2 variables:
 $ gushVM: num  2 23.2 14.3 40.9 32.8 29.1 0 79 0 0 ...
 $ n96   : AsIs [1:15, 1:96] 34.24092067 31.79041592 52.00961175 36.10294958 47.02360183 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr  "1" "2" "3" "4" ...
  .. ..$ : chr  "n96 1" "n96 2" "n96 3" "n96 4" ...

Which is exactly the same structure as the supplied datasets in package pls, and works well with plsr()

Thanks a lot for the help!
/CG


-----Ursprungligt meddelande-----
Från: CG Pettersson 
Skickat: den 17 januari 2016 13:30
Till: 'S Ellison'; Sarah Goslee
Kopia: r-help at r-project.org
Ämne: SV: [R] Problems with data structure when using plsr() from package pls

Thanks a lot to both of you for the effort and nice suggestions.

I have tested all suggested coding variants and I do succeed in changing the data structure, for example like this (with a test using str())

> names(n96) <- paste("n96", 1:96)
> frame1 <- cbind(gushVM, n96)
>  
> str(frame1)
'data.frame':   15 obs. of  97 variables:
 $ gushVM: num  2 23.2 14.3 40.9 32.8 29.1 0 79 0 0 ...
 $ n96 1 : num  34.2 31.8 52 36.1 47 ...
 $ n96 2 : num  23.3 21.1 33.6 24.3 31.5 ...
 $ n96 3 : num  97.5 80.6 89.5 97.3 122.4 ...
 $ n96 4 : num  79.2 64.4 79.9 79.4 99.7 ...
 $ n96 5 : num  24.1 25.8 45.3 25.8 45.2 ...
 $ n96 6 : num  83.1 81.7 119 82.6 140.2 ...
 $ n96 7 : num  10 10.8 16.2 10.9 15.9 ...
 $ n96 8 : num  129 124 180 135 202 ...
 $ n96 9 : num  58.8 52.9 84 61.7 76.7 ...   and so on until row variable 96 inside n96

But using this dataframe with plsr() results in the same error message, like:

> pls1 <- plsr(gushVM ~ n96, data = frame1)
Error in model.frame.default(formula = gushVM ~ n96, data = frame1) : 
  invalid type (list) for variable 'n96'

Which is the same message as ever. The same happens with the other two suggestions, even if the detail structure of "n96" inside "frame" differ a little whe I test it with str().
Still stuck, but thanks all the same!

/CG

-----Ursprungligt meddelande-----
Från: S Ellison [mailto:S.Ellison at LGCGroup.com] 
Skickat: den 15 januari 2016 14:39
Till: Sarah Goslee; CG Pettersson
Kopia: r-help at r-project.org
Ämne: RE: [R] Problems with data structure when using plsr() from package pls


> > I am trying to make pls-regression using plsr() from package pls, 
> > with Mevik & Wehrens (2007) as tutorial and the datasets from the package.
> > Everything works real nice as long as I use the supplied datasets, 
> > but I don´t understand how to prepare my own data.
> > This is what I have done:
> >
> > > frame1 <- data.frame(gushVM, I(n96))

Reading ?plsr examples and inspecting the data they use, you need to arrange frame1 so that it has the data from n96 included as columns with names of the from "n96.xxx" whre xxx can be numbers, names etc.

If n96 is a data frame, try something like
names(n96) <- paste("n96", 1:96)
frame1 <- cbind(gushVM, n96)

pls1 <- plsr(gushVM ~ n96, data = frame1)


If n96 is a matrix, 

frame1 <- data.frame(gushVM, n96=n96)

should also give you a data frame with names of the right format.

I() wrapped round a matrix or data frame does nothing like what is needed if you include it in a data frame construction, so either things have changed since the tutorial was written, or the authors were not handling a matrix or data frame with I().

S Ellison






*******************************************************************
This email and any attachments are confidential. Any use, copying or disclosure other than by the intended recipient is unauthorised. If you have received this message in error, please notify the sender immediately via +44(0)20 8943 7000 or notify postmaster at lgcgroup.com and delete this message and any copies from your computer and network. 
LGC Limited. Registered in England 2991879. 
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK


More information about the R-help mailing list