Hello, I'm trying to plot a fitted lm() line on a plot when the one explanatory variable is log transformed and log="x". I get different lines using abline and predict.lm(). #Example x <- 1:100 y <- rnorm(100) plot(y ~ x, log="x") abline(lm(y ~ log(x))) lines(x, predict(lm(y ~ log(x))), lwd=2) I'm sure I'm missing something but could someone tell me which line is correct? Thanks. Richard -- Richard Chandler, M.S. candidate Department of Natural Resources Conservation UMass Amherst (413)545-1237
Richard Chandler <rchandler at forwild.umass.edu> writes:> Hello, > > I'm trying to plot a fitted lm() line on a plot when the one > explanatory variable is log transformed and log="x". I get different > lines using abline and predict.lm(). > > #Example > x <- 1:100 > y <- rnorm(100) > plot(y ~ x, log="x") > abline(lm(y ~ log(x))) > lines(x, predict(lm(y ~ log(x))), lwd=2) > > I'm sure I'm missing something but could someone tell me which line is > correct? Thanks.Base 10 is what you're missing. The latter form is agnostic with respect to base, the former is not (since the fitted values are the same, but regression coefficients differ). So you need to know to use abline(lm(y ~ log10(x))). You don't really notice which kind of log is being used until you look at par(usr) for a plot with logged axes. -- O__ ---- Peter Dalgaard ??ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Thanks for the reply though I don't think your suggestion worked. I have found a way to get the correct line though it is not convenient. Here is a better example: x <- 1:100 y <- 1:100 plot(y ~ x, log="x") #The only way I can get the correct line is to drop the log(): abline(lm(y ~ x), untf=T, lwd=2) #or lines(x, predict(lm(y ~ x)), col=2) #Neither of these work abline(lm(y ~ log10(x))) #or abline(lm(y ~ log10(x)), untf=T) What I really would like to do is plot fitted lines and 95% confidence intervals using predict.lm, as in shown in the example, but when the predictor is log transformed and log="x". I can't figure out how to do this without removing the log() from the response part of the formula and this isn't helpful because I'm generally trying to give predict() a fitted object rather than a lm() formula. I still think I'm probably missing something simple but are there any other suggestions? Thanks. Richard Quoting Peter Dalgaard <p.dalgaard at biostat.ku.dk>:> Richard Chandler <rchandler at forwild.umass.edu> writes: > > > Hello, > > > > I'm trying to plot a fitted lm() line on a plot when the one > > explanatory variable is log transformed and log="x". I get > different > > lines using abline and predict.lm(). > > > > #Example > > x <- 1:100 > > y <- rnorm(100) > > plot(y ~ x, log="x") > > abline(lm(y ~ log(x))) > > lines(x, predict(lm(y ~ log(x))), lwd=2) > > > > I'm sure I'm missing something but could someone tell me which > line is > > correct? Thanks. > > Base 10 is what you're missing. > > The latter form is agnostic with respect to base, the former is > not > (since the fitted values are the same, but regression coefficients > differ). So you need to know to use abline(lm(y ~ log10(x))). > > You don't really notice which kind of log is being used until you > look > at par(usr) for a plot with logged axes. > > -- > O__ ---- Peter Dalgaard ??ster Farimagsgade 5, > Entr.B > c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K > (*) \(*) -- University of Copenhagen Denmark Ph: (+45) > 35327918 > ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) > 35327907 >-- Richard Chandler, M.S. candidate Department of Natural Resources Conservation UMass Amherst (413)545-1237 ----- End forwarded message ----- -- Richard Chandler, M.S. candidate Department of Natural Resources Conservation UMass Amherst (413)545-1237
Sorry that was a typo when I said 'resposnse'... I meant predictor. I want to fit lm(y ~ log(x)) and plot the line with confidence intervals on a log="x" plot so that I can see the real units of x rather than the log(x) units. I can't get the real line using predict.lm() without removing the log() from the formula. Thanks again. Quoting Peter Dalgaard <p.dalgaard at biostat.ku.dk>:> Richard Chandler <rchandler at forwild.umass.edu> writes: > > > Thanks for the reply though I don't think your suggestion worked. > I > > have found a way to get the correct line though it is not > > convenient. > > > > x <- 1:100 > > y <- 1:100 > > plot(y ~ x, log="x") > > > > #The only way I can get the correct line is to drop the log(): > > abline(lm(y ~ x), untf=T, lwd=2) #or > > lines(x, predict(lm(y ~ x)), col=2) > > > > #Neither of these work > > abline(lm(y ~ log10(x))) #or > > abline(lm(y ~ log10(x)), untf=T) > > > > What I really would like to do is plot fitted lines and 95% > > confidence intervals using predict.lm, as in shown in the > example, > > but when the predictor is log transformed and log="x". I can't > figure > > out how to do this without removing the log() from the response > part > > of the formula and this isn't helpful because I'm generally > trying to > > give predict() a fitted object rather than a lm() formula. I > still > > think I'm probably missing something simple but are there any > other > > suggestions? Thanks. > > > > First decide what you really want. I see log() hopping all over > the > place. Is it on the response or the predictor? Do you want a > straight > line on an x-logged plot or an x-logged plot of a straight line? > Do > you intend to fit y~x or y~log(x) ? > > > > > Richard > > > > > > Quoting Peter Dalgaard <p.dalgaard at biostat.ku.dk>: > > > > > Richard Chandler <rchandler at forwild.umass.edu> writes: > > > > > > > Hello, > > > > > > > > I'm trying to plot a fitted lm() line on a plot when the one > > > > explanatory variable is log transformed and log="x". I get > > > different > > > > lines using abline and predict.lm(). > > > > > > > > #Example > > > > x <- 1:100 > > > > y <- rnorm(100) > > > > plot(y ~ x, log="x") > > > > abline(lm(y ~ log(x))) > > > > lines(x, predict(lm(y ~ log(x))), lwd=2) > > > > > > > > I'm sure I'm missing something but could someone tell me > which > > > line is > > > > correct? Thanks. > > > > > > Base 10 is what you're missing. > > > > > > The latter form is agnostic with respect to base, the former > is > > > not > > > (since the fitted values are the same, but regression > coefficients > > > differ). So you need to know to use abline(lm(y ~ log10(x))). > > > > > > You don't really notice which kind of log is being used until > you > > > look > > > at par(usr) for a plot with logged axes. > > > > > > -- > > > O__ ---- Peter Dalgaard ??ster Farimagsgade 5, > > > Entr.B > > > c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. > K > > > (*) \(*) -- University of Copenhagen Denmark Ph: > (+45) > > > 35327918 > > > ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: > (+45) > > > 35327907 > > > > > > > > > -- > > Richard Chandler, M.S. candidate > > Department of Natural Resources Conservation > > UMass Amherst > > (413)545-1237 > > > > -- > O__ ---- Peter Dalgaard ??ster Farimagsgade 5, > Entr.B > c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K > (*) \(*) -- University of Copenhagen Denmark Ph: (+45) > 35327918 > ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) > 35327907 >-- Richard Chandler, M.S. candidate Department of Natural Resources Conservation UMass Amherst (413)545-1237