Ahmir
Ahmir

Reputation: 51

R-Store results from model into a matrix

I look for answers, but they do not provide what I am looking for. I am running some simple models and I am trying to get the results (LRT mainly) into a table or matrix, so I can present them better. However, the command for does not let me do that, which is odd, given other programming languages do it very easily.

In the last sentence, either if I substitute a[j] or whatever, it still says object a not found. How can I override this using for? Thank you.

li<-array(c(716, 207,79,25,819,186,67,22), c(2,2,2))
dimnames(li)<-list(c("Normal","High"),c("N BP", "H BP"),c("A","B"))
#General Model with three variables and no interactions.
a1<-loglin(li,list(1,2,3))
#Model with XY,Z
a2<-loglin(li,list(c(1,2),3))
#Model with XZ,Y
a3<-loglin(li,list(c(1,3),2))
#Model with X,ZY
a4<-loglin(li,list(c(2,3),1))
#Model with XY,XZ
a5<-loglin(li,list(c(1,2),c(1,3)))
#Model with YZ,XZ
a6<-loglin(li,list(c(2,3),c(1,3)))
#Model with XY,ZY
a7<-loglin(li,list(c(1,2),c(2,3)))

tabi<-matrix(data=c(0),nrow=10,ncol=2)
for (j in 1:7) {
  i<-j
tabi[i,1]<-a[i]$lrt
}

Side note. I have tried writing "ai", "aj", "a[i]", etc. it does not work.

Upvotes: 1

Views: 183

Answers (3)

Andrew Haynes
Andrew Haynes

Reputation: 2640

Can use paste to concatenate a, and the value of i in your loop to get a1, a2, a3 etc as a string and then use eval and parse to evaluate it as a variable.

tabi<-matrix(data=c(0),nrow=10,ncol=2)
for (i in 1:7) {

  tabi[i,1]<-eval(parse(text = paste("a",i,sep="")))$lrt

}

Upvotes: 0

Ahmir
Ahmir

Reputation: 51

For those of you who want simpler answer and had the same as I, here is a simpler way which involves less coding:

Basically just create a list and define your models as a list by adding [[]], and that's it. Run your code regularly.

a<-list()
#General Model with three variables and no interactions.
a[[1]]<-loglin(li,list(1,2,3))
#Model with XY,Z
a[[2]]<-loglin(li,list(c(1,2),3))
#Model with XZ,Y
a[[3]]<-loglin(li,list(c(1,3),2))
#Model with X,ZY
a[[4]]<-loglin(li,list(c(2,3),1))
#Model with XY,XZ
a[[5]]<-loglin(li,list(1:2,1:3))
#Model with YZ,XZ
a[[6]]<-loglin(li,list(2:3,1:3))
#Model with XY,ZY
a[[7]]<-loglin(li,list(1:2,2:3))

tabi<-matrix(data=c(0),nrow=10,ncol=2)

for (j in 1:7) {
  i<-j
tabi[i,1]<-a[[j]]$pearson
}

Upvotes: 1

user20650
user20650

Reputation: 25864

If you have all these models in your workspace you can extract then with mget, which returns a list, and then loop through this to extract the lrt.

So for your example,

sapply(mget(ls(pattern="a\\d")), "[", "lrt")

where mget(ls(pattern="a\\d")) grabs the data from the global env with the pattern anumber, and sapply loops through this list to extract.

As an alternative workflow, rather than having lots of models floating about in your workspace you could store the models in a list.

So store the parameters in a list

para <- list(list(1,2,3), list(c(1,2),3), list(c(1,3),2))

Then loop through these applying the model

lst <- lapply(para, function(x) loglin(li, x))

This stores each model in lst. You could of used loglin(li, x)$lrt above if you just want this item. Otherwise you can loop through as before to extract it.

sapply(lst, "[", "lrt")

Note about your code is that a[1] is not the same as a1

Upvotes: 3

Related Questions