Skip to content

do glm with two data sets

3 messages · Hu, Ying (NIH/NCI), Sundar Dorai-Raj, Gavin Simpson

#
Thanks for your help.

# 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 
d1<-data.frame(g[1,], e[1,])
fit<-glm(e[1,] ~ g[1,], data=d1)
summary(fit)

I am not sure that is the best solution.

Thanks again,

Ying
 

-----Original Message-----
From: Gavin Simpson [mailto:gavin.simpson at ucl.ac.uk] 
Sent: Wednesday, August 17, 2005 7:01 PM
To: Sundar Dorai-Raj
Cc: Hu, Ying (NIH/NCI); r-help at stat.math.ethz.ch
Subject: Re: [R] do glm with two data sets
On Wed, 2005-08-17 at 17:22 -0500, Sundar Dorai-Raj wrote:
Hi Ying,

That error message is likely caused by having a data.frame on the right
hand side (rhs) of the formula. You can't have a data.frame on the rhs
of a formula and g1 is still a data frame even if you only choose the
first row, e.g.:

dat <- as.data.frame(matrix(100, 10, 10))
class(dat[1, ])
[1] "data.frame"

You could try:

glm(e1 ~ ., data=g1[1, ])

and see if that works, but as Sundar notes, your post is a little
difficult to follow, so this may not do what you were trying to achieve.

HTH

Gav
http://www.R-project.org/posting-guide.html
#
Hu, Ying (NIH/NCI) wrote:
Hi, Ying,

What's wrong with this solution? Do you still get an error? What is your 
primary goal?

A couple of points:

1. It's better to use names in your data.frame:

d1 <- data.frame(g = g[1,], e = e[1,])

Then in glm:

fit <- glm(e ~ g, data = d1)

2. Also, you may just be giving us a toy example, but if you don't 
specify a family argument in glm then you are simply getting the least 
squares. In that case you should use ?lm instead.

HTH,

--sundar
#
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