Search code examples
rlinear-programmingsimplex

Getting "NAs are not allowed in subscripted assignments" while using Simplex in R


I am running the following command:

>simplex(a = a, A2 = A2, b2 = b2, A3 = A3, b3 = b3)

on which I get the error:

Error in pivot(tableau, prow, pcol) : 
  NAs are not allowed in subscripted assignments

Here is my R script:

library("boot")     # For simplex

set.seed(100)

i <- 6
X <- rnorm(i, mean = 4, sd = 1)
e <- rnorm(i, mean = 0, sd = 1)
Y <- 5*X + e

sum <- 0
Y_a <- matrix(0,1,floor(i/2))                 
for(miss in 1:floor(i/2)){
    sum <- sum + Y[miss*2]
    Y_a[miss] <- Y[miss*2]
}

Y_p <- 5*rnorm(floor(i/2), mean = 4, sd = 1) + rnorm(floor(i/2), mean = 0, sd = 1)          
a <- matrix(0,1,2*floor(i/2))
for(miss in 1:floor(i/2)){
    a[miss] <- 1
}
A3 <- t(apply(a,1, rev))
A2 <- matrix(0,2*floor(i/2),2*floor(i/2))

for(miss in 1:floor(i/2)){
    index <- 2*miss
    index_ <- index - 1
    A2[index_,miss] <- 1
    A2[index_,miss+floor(i/2)] <- 1
    A2[index,miss] <- 1
    A2[index,miss+floor(i/2)] <- -1
}
b2 <- matrix(0,2*floor(i/2),1)
b2[(1:(2*floor(i/2)))%%2==1] <- Y_p
b2[(1:(2*floor(i/2)))%%2==0] <- -Y_p
simplex(a = a, A2 = A2, b2 = b2, A3 = A3, b3 = sum)

I get the above error when variable "i" is larger than 5, otherwise I get the error:

Error in simplex1(c(a, rep(0, m1 + 2 * m2 + m3)), cbind(rbind(A1, A2,  : 
  number of items to replace is not a multiple of replacement length
In addition: Warning message:
In simplex1(c(a, rep(0, m1 + 2 * m2 + m3)), cbind(rbind(A1, A2,  :
  number of items to replace is not a multiple of replacement length

I can't understand if these errors mean that the LP problem can't be solved or mean that there is a mistake in the way the function is called.

Thanks in advance.


Solution

  • Documentation of ?simplex for argument b2 states that

    A vector of length m2 giving the right hand side of the >= constraints. This argument is required if A2 is given and ignored otherwise. All values in b2 must be non-negative. Note that the constraints x >= 0 are included automatically and so should not be repeated here.

    Try ensuring b2 is always non-negative.