I am a beginner in R. I have two matrices with the same number of rows (let's say 10), and many columns. I want to do a linear regression, using glm, between eacg row of matrixA and the corresponding row of matrixB. I want to print the residuals in a new matrix, which will have the same number of rows as the original matrices:
matrixA <- read.table("fileA.txt", header=TRUE, row.names=1)
matrixB <- read.table("fileB.txt", header=TRUE, row.names=1)
for(i in 1:10) {
response = as.matrix(matrixA)[i,]
predictor = as.matrix(matrixB)[i,]
fit <- glm(response ~ predictor)
residuals[i,] <- fit$residuals
}
However, I am getting this error:
Error in residuals[1, ] <- fit$residuals :
incorrect number of subscripts on matrix
I looked up this error a bit, and thought that maybe it did not recognize fit$residuals as a vector, so I tried to specify it (as.vector(fit$residuals)), but that did not fix it.
Any idea on how I can fix this? Thank you!
Format of the matrices (both have the same format)
a b c d f
A 1.0 2.0 3.0 4.0 5.0
B …
C
D
E
F
G
H
I
J
residuals
come from? Do you really want it to be 2 Dimensional? – Benjamin Mohn