Matrix Error: Requires numeric in R -


phi <- as.matrix(cbind(1, train.data)) # add column of 1 phi_0 train.len <- 75 eta <- 0.01 # learning rate epsilon <- 0.001 #  tau.max <- 75 # maximum number of iterations  t <- ifelse(train.label == c0, eval(parse(text=c0)),eval(parse(text=c1))) labels  w <- matrix(,nrow=tau.max, ncol=ncol(phi)) # empty weight vector w[1,] <- runif(ncol(phi)) # random initial values weight vector  error.trace <- matrix(0,nrow=tau.max, ncol=1) # placeholder errors error.trace[1] <- sum((phi%*%w[1,])*t<0)/train.len*100  

train.data looks like:

x1    x2    x3    x4    y 5.1   3.5   1.4   0.2   c1 4.7   3.2   1.3   0.2   c1 35.0  3.6   1.4   0.2   c1 

the problem happens in last line. checked other posts (matrix expression causes error "requires numeric/complex matrix/vector arguments"?) . ensure data in matrix format , checked both numeric (phi , w).

the error

error in phi %*% w[1, ] : requires numeric/complex matrix/vector arguments

i no error when generate toy data: train.data <- rnorm(75). train.data? data frame? if so, sapply(train.data, class)? suspect have factors / characters. in case, matrix phi character matrix, instead of numerical one.

maybe should try using:

phi <- cbind(1, data.matrix(train.data)) 

read ?data.matrix more. way use:

phi <- cbind(1, sapply(train.data, as.numeric)) 

update

indeed have column y, either factor or character column. solution above work. but, don't know context, should ask whether makes sense include y column computation. if don't want it, drop , use:

phi <- cbind(1, as.matrix(train.data[,1:4])) 

Comments