0

I am trying to run a polynomial regression using limSolve based on the example here.

My code is the following.

hr <- c(9.0, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0,
        16.5, 17.0, 17.5, 18.0, 18.5, 19.0, 19.5, 20.0, 20.5, 21.0, 21.5, 22.0, 22.5, 23.0, 23.5,
        24.0, 24.5, 25.0, 25.5, 26.0, 26.5, 27.0, 27.5, 28.0, 28.5, 29.0, 29.5, 30.0, 30.5, 31.0,
        31.5, 32.0, 32.5, 33.0, 33.5, 34.0, 34.5, 35.0, 35.5, 36.0, 36.5, 37.0, 37.5, 38.0, 38.5,
        39.0, 39.5)

fq <- c( 305, 310, 303, 236, 266, 241, 268, 222, 235, 230, 189, 191, 193, 162, 184, 170, 145, 147, 165,
         142, 155, 158, 130, 135, 122, 125, 126, 131, 117, 109, 112, 122, 104, 101, 76, 102, 97, 82,
         78,  78,  62,  96,  77,  73,  71,  81,  86,  85, 81, 68, 64, 73, 69, 53,  61, 66, 54,
         55,  46,  53,  48,  65)


n <- 5

# create polynomials:
for(i in 0:n) { 
  assign(paste0("poly", i), (hr^i))
}

library(Matrix)
library(limSolve)
A <- do.call(cbind, lapply( ls(patt="poly"), get) )
b <- fq
h <- rep(0,4)
B_c <- lsei(A = A, B = b, H = h, type=2)
dat$pred <- A%*%B_c$X

I get the following error msg:

Error in lsei(A = A, B = b, H = h, type = 2) : 
  cannot solve least squares problem - G and H not compatible

Why does this happen? What am I doing wrong?

EDIT: The reason I am using limSolve instead of simple lm is that I need to put an inequality constraint on my regressions later.

Stata_user
  • 562
  • 3
  • 14
  • You've not defined a `G` variable? You've specified `h`, but that's meant to be the right hand side of a constraints equation `Gx >= h`; `h` is insufficient by itself. – user2474226 Jan 06 '20 at 11:48
  • Yes, that's true. My mistake. Let me post again with the full constraints. – Stata_user Jan 06 '20 at 12:11

1 Answers1

0

Taking out the reference to H solves this problem. Like so:

B_c <- lsei(A = A, B = b, type=2)
Stata_user
  • 562
  • 3
  • 14