Skip to content

do glm with two data sets

2 messages · Hu, Ying (NIH/NCI), Sundar Dorai-Raj

#
You are right. 
# read the two data sets
e <- as.matrix(read.table("file1.txt", header=TRUE,row.names=1))
g <- as.matrix(read.table("file2.txt", header=TRUE,row.names=1))

# solution 2
summary(glm(e[1,] ~ g[1,]))
summary(glm(e[1,] ~ g[2,]))
...
They work very well.

If I put it in the loop, such as

for (i in 1:50){
  for (j in 1:50){
     cat("file1 row:", i, "file2 row:", j, "\n")
     print(summary(glm(e[i,] ~ g[j,])))
  }
} 

Why do I have to use "print" to print the results? If without "print"
for (i in 1:50){
  for (j in 1:50){
     cat("file1 row:", i, "file2 row:", j, "\n")
     summary(glm(e[i,] ~ g[j,]))
  }
}
then without the results of glm.

Thanks a lot.

Ying
 

-----Original Message-----
From: Gavin Simpson [mailto:gavin.simpson at ucl.ac.uk] 
Sent: Thursday, August 18, 2005 11:00 AM
To: Hu, Ying (NIH/NCI)
Cc: Sundar Dorai-Raj; r-help at stat.math.ethz.ch
Subject: RE: [R] do glm with two data sets
On Thu, 2005-08-18 at 10:38 -0400, Hu, Ying (NIH/NCI) wrote:
This is redundant, as:
and:

fit <- glm(e[1, ] ~ g[1, ])

are equivalent - you don't need data = d1 in this case, e.g:

e <- matrix(c(0, 1, 0, 0, 1, 1, 1, 1, -1), ncol = 3, byrow = TRUE)
e
g <- matrix(c(1.22, 1.34, 2.44, 2.33, 2.56, 2.56, 1.56, 1.99, 1.46),
ncol = 3, byrow = TRUE)
g
fit <- glm(e[1, ] ~ g[1, ])
fit

works fine.
This seems a strange way of doing this. Why not:

pred <- g[1, ]
resp <- e[1, ]
fit <- glm(resp ~ pred)
fit

and do your subsetting outside the glm call - makes things clearer no?
Unless you plan to do many glm()s one per row of your two matrices. If
that is the case, then there are better ways of approaching this.
HTH

G
#
Hu, Ying (NIH/NCI) wrote:
This is a FAQ 7.16.

See http://cran.r-project.org/doc/FAQ/R-FAQ.html

--sundar