1

I’m trying to implement the Partial Least Squares Regression model in R using the pls package. The following data frame represents my data, where I have two response variables and three explanatory variables.

library(pls)
df <- data.frame(x1 = rnorm(20),
                 x2 = rnorm(20),
                 x3 = rnorm(20),
                 y1 = rnorm(20),
                 y2 = rnorm(20))
model <- plsr(c(y1, y2) ~ x1+x2+x3, ncomp = 3, scale = TRUE, data = df)

However, when I run the model, I always get an error message.

Error in model.frame.default(formula = c(y1, y2) ~ x1 + x2 + x3, data = df) : variable lengths differ (found for 'x1')

I believe that the data frame is not correctly structured. Would anyone have any suggestions?

user438383
  • 5,716
  • 8
  • 28
  • 43
aa3ls
  • 115
  • 4
  • 1
    ``c(y1, y2)`` is longer than ``x``. The vectors on either side of ``~`` should all be the same length. I'm not familiar with plsr, but you perhaps want to ``cbind(y1, y2)`` to join them as 2 column vectors, rather than concatenate them into a single vector – user438383 Sep 22 '22 at 23:00

0 Answers0