[R] GLM Coding Issue

Peter Ehlers ehlers at ucalgary.ca
Wed Nov 28 02:05:15 CET 2012


On 2012-11-27 14:34, Steve Lianoglou wrote:
> Hi,
>
> Comments inline:
>
> On Tue, Nov 27, 2012 at 1:00 PM, Craig P O'Connell
> <coconnell2 at umassd.edu> wrote:
>>
>>
>> Dear all,
>>
>>     I am having a recurring problem when I attempt to conduct a GLM.  Here is what I am attempting (with fake data):
>> First, I created a txt file, changed the directory in R (to the proper folder containing the file) and loaded the file:
>>
>> #avoid<-read.table("avoid.txt",header=TRUE);avoid
>> #  treatment feeding avoid noavoid
>> #1   control  nofeed     1     357
>> #2   control    feed     2     292
>> #3   control     sat     4     186
>> #4      proc  nofeed    15     291
>> #5      proc    feed    25     288
>> #6      proc     sat    17     140
>> #7       mag  nofeed    87     224
>> #8       mag    feed    34     229
>> #9       mag     sat    46     151
>>
>> I then try to "attach(avoid)" the data, but continue to get an error message ( The following object(s) are masked _by_ .GlobalEnv :), so to fix this, I do the following:
>>
>> #newavoid<-avoid
>> #newavoid                (does this do anything?)
>
> It essentially makes a copy of `avoid` to `newavoid` -- what did you
> want it to do?
>
> That having been said, a good rule of thumb is to never use `attach`,
> so let's avoid it for now.
>
>> Lastly, I have several GLM's I wanted to conduct.  Please see the following:
>>
>> #model1<-glm(cbind(avoid, noavoid)~treatment,data=,family=binomial)
>>
>> #model2=glm(cbind(avoid, noavoid)~feeding, familiy=binomial)
>>
>> #model3=glm(cbind(avoid, noavoid)~treatment+feeding, familiy=binomial)
>
> `cbind`-ing doesn't make much sense here. What is your target (y)
> variable here? are you trying to predict `avoid` or `noavoid` status?
>
> Let's assume you were "predicting" `noavoid` from just `treatment` and
> `feeding` (I guess you have more data (rows) than you show), you would
> build a model like so:
>
> R> model <- glm(noavoid ~ treatment + feeding, binomial, avoid)
>
> Or to be explicit about the parameters:
>
> R> model <- glm(noavoid ~ treatment + feeding, family=binomial, data=avoid)
>
>
>> It would be greatly appreciated if somebody can help me with my coding, as you can see I am a novice but doing my best to learn.  I figured if I can get model1 to run, I should be able to figure out the rest of my models.
>
> Since you're just getting started, maybe it would be helpful for
> people writing documentation/tutorials/whatever what needs to be
> explained better.
>
> For instance, I'm curious why you thought to `cbind` in your first glm
> call, which was:
>
> model1<-glm(cbind(avoid, noavoid)~treatment,data=,family=binomial)
>
> What did you think `cbind`-ing was accomplishing for you? Is there an
> example somewhere that's doing that as the first parameter to a `glm`
> call?


Steve:
re a matrix response: see MASS (the book, 4ed) page 191; also found
in the ch07.R file in the /library/MASS/scripts folder. I seem to
recall that this is mentioned somewhere in the docs, but put my finger
on it now.

One additional comment about the analysis: overdispersion might be
a problem.

Peter Ehlers

[...]




More information about the R-help mailing list