rarraysmatrixsna

Fill in fitted and residual values from netlm into respective cells in matrix data


I'm using netlm() from the sna package to regress one variable on another in order to get fitted and residual values. I set mode argument inside netlm() to undirected "graph" with 10 times QAP. Then I checked the output.

The output contains 12,880 fitted and residual values and, given that the dimension of my data is 161 * 161, this means that they represent the fitted/residual values for the lower (or upper) diagonal matrix data minus main diagonal values ((161*161)/2 - 161 = 12880). If I want to generate a new matrix of the same dimension as my data and filled both upper and lower diagonals with fitted values, how I can make sure that those values will actually be filled into their corresponding cells?

It will be really appreciated if someone could shed some light on this. I provide my code at below for your reference.

library(sna)


x <- readRDS(url("https://www.dropbox.com/s/v1kz95luew28o7u/xki_ratio2019.rds?dl=1"))
y <- readRDS(url("https://www.dropbox.com/s/yc7md37x6albxog/xki_lag.rds?dl=1"))

model.nl <- netlm(y[[1]], list(x), diag = FALSE, mode = "graph", nullhyp=c("qapx"), reps = 100)

dim(x)
161 161

length(model.nl$residuals)
12880


Solution

  • Something like

    resid <- matrix(NA, 161, 161)
    resid[upper.tri(resid)] <- model.nl$residuals
    

    Not sure whether you want upper.tri or lower.tri off the top of my head, and you might need to transpose