[R] help with predict.glm, and charting with factors
ONKELINX, Thierry
Thierry.ONKELINX at inbo.be
Wed Dec 12 17:17:31 CET 2012
Dear Chad,
Did you post your entire dataset? If so:
1) your model is too complex for the amount of data you have. See the quotes below...
2) There is complete separation, leading to large parameter estimates and fits very close to 0 and 1 (in terms of probabilities)
3) You fit temperature as a factor, thus removing all qualitative aspects and ordering in that variable. Though this makes sense given your experiment, it removes the possibility to make predictions for other temperatures.
4) Learn not to use attach(). It will bite you if you don't use it with extreme care.
5) Try to get advise from a local statistician.
Best regards,
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry.Onkelinx at inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Chad Widmer
Verzonden: woensdag 12 december 2012 13:50
Aan: r-help at r-project.org
Onderwerp: [R] help with predict.glm, and charting with factors
Dear R Wizards,
After much frustration and days of confusion I have finally broken down and am asking for help, which I don't like doing, but I just can't figure this one out on my own. I've conducted a laboratory experiment testing the effects of temperature and salinity on whether or not a biological event will occur (Go or NoGo). I've coded the factors temperature and salinity as factors for the binomial glm, and I haven't had any trouble fitting the model and checking assumptions.
I am however having trouble with the predict.glm function. I want to create a graph using my data that is similar to the one produced by the budworm example at the bottom of the predict.glm R documentation. In my case I want temperature on the x axis, probability on the y axis, and the lines on the graph to represent the probability of the event occurring at the different salinities tested at the different temperatures.
I created a smaller version of my data and have included it and the R code I used below. I get two main problems that I hope you're willing to help with.
1. When I input the text argument the output in the graph gives salinity values that are superimposed on one another. And, the values don't seem to make sense - they are returned as probabilities of either 1.0 or 0.
2. When I input the lines argument I get the following error messages:
Error: variable 'fTemp' was fitted with type "factor" but type "numeric"
was supplied
In addition: Warning message:
In model.frame.default(Terms, newdata, na.action = na.action, xlev =
object$xlevels) : variable 'fTemp' is not a factor
grrrrrrrrrr
Pleasehelp<-read.table("Rhelp.txt",h=T)
attach(Pleasehelp)
fix(Pleasehelp)
Temp Sal Go Total
5 34 1 1
5 34 1 1
5 34 1 1
5 21 1 1
5 21 1 1
5 21 0 1
10 34 1 1
10 34 0 1
10 34 1 1
10 21 1 1
10 21 0 1
10 21 0 1
15 34 0 1
15 34 0 1
15 34 0 1
15 21 0 1
15 21 0 1
15 21 0 1
fTemp<-factor(Temp)
fSal<-factor(Sal)
Go<-Go
NoGo<-Total-Go
Went<-cbind(Go,NoGo)
DF<-data.frame(fTemp,fSal,Went,Total)
glm<-glm(Went~fTemp+fSal+fTemp*fSal,family="binomial",data=DF)
require(graphics)
plot(c(5,15),c(0,1),type="n",xlab="Temperature",ylab="Probability of going")
text(Temp,Go/Total,as.character(Sal))
ld<-(seq(5,15,1))
lines(ld,predict(glm,data.frame(fTemp=ld,fSal=factor(rep("34",length(ld)),levels=levels(fSal))),type="response"))
Thank you very much for your time and expertise!
Kindly,
Chad
[[alternative HTML version deleted]]
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.
More information about the R-help
mailing list