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?

1
  • 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 Commented Sep 22, 2022 at 23:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.