Reputation: 27
I want to do a somewhat large number of Seemingly Unrelated Regressions (SUR) in R from the 'systemfit' package. To facilitate toying around with the number of variables included i want to automate the process. I run into an error however when using a for loop to run the regressions, while the manual way does not give me any errors. I use the code below. The error I recieve is:
Error in solve(sigma, tol = solvetol) :
Lapack routine dsptrf returned error code 11
I use the code below.
region=12
vars=4
# Performing Seemingly Unrelated Regression per variable and every region
for (i in 1:vars){
system <- list()
for (j in 1:region){
eq_single <- data_mat[,j]~data_mat[,j+12] + data_mat[,j+24] + data_mat[,j+36]
system[[j]] <- eq_single
}
sur <- systemfit(system, method="SUR")
}
# Manually performing Seemingly Unrelated Regression for 2 regions only
Y1 <- data_mat[,1]
Y2 <- data_mat[,region]
X2 <- cbind(data_mat[,j+region] + data_mat[,26])
eq1 <- Y1 ~ data_mat[,13] + data_mat[,25]
eq2 <- Y2 ~ X2
system <- list(eq1=eq1, eq2=eq2)
sur <- systemfit(system, method="SUR")
summary(sur)
Im very inexperienced at R and am sure I'm doing something wrong. If so, what am I doing wrong?
Thanks in advance!
Upvotes: 0
Views: 434
Reputation: 1064
Formulas in R are not evaluated, so j+12
is stored as exactly the same: "j+12"
and not 13
, 14
, etc. That's why you ended up having the same formula multiple times in eq_single
.
Also note, that vars
is not used in your current code.
library(systemfit)
region=12
vars=4
# Performing Seemingly Unrelated Regression per variable and every region
# for (i in 1:vars){ # i is not used in the loop
system <- list()
for (j in 1:region){
# using paste0() to create correct formulas
eq_single <- formula(paste0('data_mat[,',j,'] ~ data_mat[,',j+12,'] +
data_mat[,',j+24,'] + data_mat[,',j+36,']'))
system[[j]] = eq_single
}
sur <- systemfit(system,method="SUR")
# }
Upvotes: 0