Gabriel
Gabriel

Reputation: 425

Error in multiple regression: number of items to replace is not a multiple of replacement length

I am trying to split my data into training and test data using a code obtained from my professor but am getting errors. I thought it was because of the data's format, but I went back to hard code it and nothing works. The data is in matrix form right now and I believe the code is being used to predict how accurate the logistic regression is.

A = matrix(
  c(64830,18213,4677,24761,9845,17504,22137,12531,5842,28827,66161,18852,5581,27219,10159,17527,23402,11409,8115,31425,68426,18274,5513,25687,10971,14104,19604,13438,6011,30055,69716,18366,5735,26556,11733,16605,20644,15516,5750,31116,73128,18906,5759,28555,11951,19810,22086,17425,6152,28469,1,1,1,0,1,0,0,0,0,1),

nrow = 10,
ncol = 6,
byrow = FALSE)


n<-row(A);
K<-ncol(A)-1;
x<-matrix(0,n,K);

for(i in 1:K){x[,i]<-A[,i];}
#A[,i] is 10long and x[,i] is 1long.
A[,i:length(x[,i])]=x[,i]
y<-A[,K+1];
#training/test data split:
idx<-sample(1:n,floor(n/2),replace=FALSE);
xtr<-x[idx,]; ytr<-y[idx];
xts<-x[-idx,]; yts<-y[-idx];
#fit the logistic model to it
myglm<-glmnet(xtr,ytr,family = "binomial"); 
#Error in if (is.null(np) | (np[2] <= 1)) stop("x should be a matrix with 2 or more columns") : argument is of length zero

#apply traning data to test data
mypred<-predict(myglm,newx=xts,type="response",s=0.01);
posteriprob<-mypred[,,1];
yhat<-matrix(1,nrow(xts),1);
for(i in 1:nrow(xts))
{
  yhat[i]<-which.max(posteriprob[i,]);
}

acc<-sum(yhat+2==yts)/nrow(xts);
cat("accuracy of test data:", acc, "\n");

The first forloop gives me this error: Error in x[, i] <- A[, i]:

Number of items to replace is not a multiple of replacement length

When I run the logistic model using xtr/ytr I get error in if (is.null(np) | (np[2] <= 1)) stop("x should be a matrix with 2 or more columns"):

argument is of length zero

Upvotes: 0

Views: 935

Answers (1)

zyurnaidi
zyurnaidi

Reputation: 2273

For the first error, it was a typo. Change n<-row(A) to n<-nrow(A) and it should've worked. But after that, A[,i:length(x[,i])]=x[,i] produces other error, since the size of A is 10x6 while the length(x[,i]) is 10. Probaby you wanted to do different thing here, than what is currently coded.

For the second error, the xtr should have a size of at least n x 2. Also, your data is not appropriate for binomial glm. Observations should be either 1 or 0.

Upvotes: 1

Related Questions