[R] help with predict and plotting confidence intervals

Michael Denslow mwdenslow at yahoo.com
Thu Mar 12 19:42:59 CET 2009


Thank you for you help Dr. Winsemius.

The problem seems to stem from the fact that I have used the incorrect name in the prediction dataframe.
The following code seems to work correctly. 
Thank you again,
Michael

wt.data <- data.frame(code = factor(LETTERS[1:24]),
	area = c(60865,480,656792,92298,1200,1490,8202,4000,220,245,4000,390,325,
	16,162911,20235,68800,3389,7,696,4050,1498,1214,99460),
	species = c(673,650,1353,1026,549,536,782,734,516,580,673,560,641,443,1105,
	871,789,575,216,407,942,655,582,1018))

wt.data$logA <- log10(wt.data$area)
wt.data$logS <- log10(wt.data$species)
wt.mod <- lm(logS~logA, data = wt.data)

with(wt.data,plot(logA,logS, ylim = c(2.0,3.5),xlim = c(0,6)))
pred.frame <- data.frame(logA = seq(0,6, length.out = 24))
pp <- predict(wt.mod, int = "p", newdata=pred.frame)
matlines(pred.frame$logA,pp, lty=c(1,2,2),col="red")




Michael Denslow

Graduate Student
I.W. Carpenter Jr. Herbarium [BOON]
Department of Biology
Appalachian State University
Boone, North Carolina U.S.A.

-- AND --

Communications Manager
Southeast Regional Network of Expertise and Collections
sernec.org
> >> 
> >> # I am not sure how to get the lines to draw
> correctly here
> >> matlines(pred.frame$a,pp,
> lty=c(1,2,2),col="black")
> >> 
> > The x values are your sequence whereas the y values
> are in the sequence from the original data. They are not
> correctly associated with each other.
> > 
> > Try:
> > pp <- predict(lm(wt.data$logS~wt.data$logA), int =
> "p", newdata= data.frame(logA=seq(0,6, length.out
> = 24))  )
> > plot(pp)
> > 
> At this point I should not have accepted your starting
> point. A better starting point would be to use the wt.mod
> model:
> 
> pp <- predict(wt.mod, int = "p", newdata=
> list(logA=seq(0,6, length.out = 24)) )
> 
> # Followed by:
> 
> plot( seq(0,6, length.out = 24),  pp[ ,"fit"] )
>  lines(seq(0,6, length.out = 24),  pp[ ,"lwr"],
> lty=2)
>  lines(seq(0,6, length.out = 24),  pp[ ,"upr"],
> lty=2)
> 
> 
> > --david winsemius
> > 
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained,
> reproducible code.
> 
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT




More information about the R-help mailing list