Matrix Expression Causes Error "Requires Numeric/Complex Matrix/Vector Arguments"

Matrix expression causes error requires numeric/complex matrix/vector arguments?

To get the matrix multiplication to work, you need to convert the data.frame (presumably that's what da is) to a matrix. Calculating the transpose with t() automatically does this:

t(da)%*%as.matrix(da)

But this gives a 7x7 matrix which can't be added to the 3x3 identity matrix that you're using. Do you mean something like:

ma=diag(7)+t(da)%*%as.matrix(da)

You may like to have a look at An Introduction to R if you don't feel confident about the difference between a matrix and data.frame.

requires numeric/complex matrix/vector arguments error in matrix multiplication with matrices of correct shapes and sizes

The vec that you created is a column of lists (because of your lapply call). You can see this by inspecting the first element:

vec[1, ]

[[1]]
[1] 1

The alternative would be to feed your lambda vector directly to rpois:

vec <- rpois(12, lambda = 1:12)
B <- matrix(1, nrow = 7, ncol = 12)
B %*% vec

[,1]
[1,] 87
[2,] 87
[3,] 87
[4,] 87
[5,] 87
[6,] 87
[7,] 87

Error: requires numeric/complex matrix/vector arguments for %*%; cross validating glmmTMB model

Without looking at the code too carefully: glmmTMB::fixef(reg) returns a list (with elements cond (conditional model parameters), zi (zero-inflation parameters), disp (dispersion parameters) rather than a vector.

If you replace this bit with glmmTMB::fixef(reg)[["cond"]] it will probably work.

Matrix multiplication in R: requires numeric/complex matrix/vector arguments

Matrix-multiplication operators / functions like "%*%", crossprod, tcrossprod expect matrices with "numeric", "complex" or "logical" mode. However, your matrix has "character" mode.

library(mlbench)
data(BreastCancer)
X <- as.matrix(BreastCancer[, 1:10])
mode(X)
#[1] "character"

You might be surprised as the dataset seems to hold numeric data:

head(BreastCancer[, 1:10])
# Id Cl.thickness Cell.size Cell.shape Marg.adhesion Epith.c.size
#1 1000025 5 1 1 1 2
#2 1002945 5 4 4 5 7
#3 1015425 3 1 1 1 2
#4 1016277 6 8 8 1 3
#5 1017023 4 1 1 3 2
#6 1017122 8 10 10 8 7
# Bare.nuclei Bl.cromatin Normal.nucleoli Mitoses
#1 1 3 1 1
#2 10 3 2 1
#3 2 3 1 1
#4 4 3 7 1
#5 1 3 1 1
#6 10 9 7 1

But you are misinformed by the printing style. These columns are in fact characters or factors:

lapply(BreastCancer[, 1:10], class)
#$Id
#[1] "character"
#
#$Cl.thickness
#[1] "ordered" "factor"
#
#$Cell.size
#[1] "ordered" "factor"
#
#$Cell.shape
#[1] "ordered" "factor"
#
#$Marg.adhesion
#[1] "ordered" "factor"
#
#$Epith.c.size
#[1] "ordered" "factor"
#
#$Bare.nuclei
#[1] "factor"
#
#$Bl.cromatin
#[1] "factor"
#
#$Normal.nucleoli
#[1] "factor"
#
#$Mitoses
#[1] "factor"

When you do as.matrix, these columns are all coerced to "character" (see R: Why am I not getting type or class "factor" after converting columns to factor? for a thorough explanation).

So to do the matrix-multiplication, we need to correctly coerce these columns to "numeric".



dat <- BreastCancer[, 1:10]

## character to numeric
dat[[1]] <- as.numeric(dat[[1]])

## factor to numeric
dat[2:10] <- lapply( dat[2:10], function (x) as.numeric(levels(x))[x] )

## get the matrix
X <- data.matrix(dat)
mode(X)
#[1] "numeric"

Now you can do for example, a matrix-vector multiplication.

## some possible matrix-vector multiplications
beta <- runif(10)
yhat <- X %*% beta

## add prediction back to data frame
dat$prediction <- yhat

However, I doubt this is the correct way to obtain predicted values for you logistic regression model as when you build your model with factors, the model matrix is not the above X but a dummy matrix. I highly recommend you using predict.



This line also worked for me: as.matrix(sapply(dat, as.numeric))

Looks like you were lucky. The dataset happens to have factor levels as same as numeric values. In general, converting a factor to numeric should use the method I did. Compare

f <- gl(4, 2, labels = c(12.3, 0.5, 2.9, -11.1))
#[1] 12.3 12.3 0.5 0.5 2.9 2.9 -11.1 -11.1
#Levels: 12.3 0.5 2.9 -11.1

as.numeric(f)
#[1] 1 1 2 2 3 3 4 4

as.numeric(levels(f))[f]
#[1] 12.3 12.3 0.5 0.5 2.9 2.9 -11.1 -11.1

This is covered at the doc page ?factor.

r error : requires numeric/complex matrix/vector arguments?

The vector mu is a data.frame. Try converting that to a matrix.

as.matrix(mu[1,]) %*%  solve(sigma)
# Sepal.Length Sepal.Width Petal.Length Petal.Width
#95 16.37781 11.46013 -0.198358 -9.473886

Error: requires numeric/complex matrix/vector arguments when using matrix times vector multiplication

This works fine. You must have some NAs in your cpi_calc table. Try na.omit(cpi_calc)

cpi_calc <- read.table(text="0.358  0.359   0.06    0.419   0.191   0.296
100 100 100 100 100 100
99.99 100 100.07 100.01 100.8 101.59
99.52 99.58 99.94 100.01 101.03 101.38
99.46 99.44 99.85 100.01 101.03 101.03
99.13 99.37 99.79 99.97 101 101.82",header=FALSE)

as.matrix(cpi_calc[2:6, 1:6]) %*% t(cpi_calc[1, 1:6])
1
2 168.3000
3 168.9282
4 168.5832
5 168.4024
6 168.4669

Getting requires numeric/complex matrix/vector arguments error using neuralnet package - R

We can change the threshold value from the default 0.01 as the default value is causing model convergence issues. Based on the documentation for ?neuralnet

threshold - a numeric value specifying the threshold for the partial derivatives of the error function as stopping criteria.

Using that info, modify the value to another one i.e. 0.02

library(neuralnet)
model1 <- neuralnet(realized1~ann_t1+ann_t2+ann_t3, data=df_1, hidden=3,
threshold = 0.05, act.fct = "logistic",linear.output = TRUE)
y <- as.data.frame(predict(model1,df_2))
str(y)
#'data.frame': 100 obs. of 1 variable:
# $ V1: num -1.06 1.31 -1.13 -1.05 -1.06 ...

glmmLasso error requires numeric/complex matrix/vector arguments

It appears, that by default, glmmLasso function specifies the random effects with the same formula from the fixed effects (i.e. glmmLasso(fix=formula, rnd=formula, ...).

To run it without random effect estimation, use rnd=NULL:

> lasso_fe <- glmmLasso(
y~x+as.factor(ID)+as.factor(year),
rnd = NULL, # <- no r.e.
family=binomial(link = logit), lambda=10, data = df)

> lasso_fe
Call:
glmmLasso(fix = y ~ x + as.factor(ID) + as.factor(year), rnd = NULL,
data = df, lambda = 10, family = binomial(link = logit))

Fixed Effects:

Coefficients:
(Intercept) x as.factor(ID)2 as.factor(ID)3
-0.09531017 0.00000000 0.00000000 0.00000000
as.factor(ID)4 as.factor(ID)5 as.factor(ID)6 as.factor(ID)7
0.00000000 0.00000000 0.00000000 0.00000000
as.factor(year)2 as.factor(year)3
0.00000000 0.00000000

No random effects included!

The error happens, because the package assumes random effects to be normally distributed. Factor variables do not fit to such specification, since they are not numeric.



Related Topics



Leave a reply



Submit