r - Problems displaying LOESS regression line and confidence interval -
i having issues trying compete loess regression data set. have been able create line, unable plot correctly.
i ran through data this.
animals.lo <- loess(x15p5 ~ period, animals, weights = n.15p5) animals.lo summary(animals.lo) plot(x15p5~ period, animals) lines(animals$x15p5, animals.lo, col="red")
at point received error
"error in xy.coords(x, y) : 'x' , 'y' lengths differ"
i searched around , read issue due points needing ordered, proceeded.
a <- order(animals$period) lines(animals$x15p5[a], animals.lo$period[a], col="red", lwd=3)
there no errors @ point, loess line still not showing in plot. points displayed correctly, not line.
this similar data set using...
structure(list(site = c("cat", "dog", "bear", "chicken", "cow", "bird", "tiger", "lion", "leopard", "wolf", "puppy", "kitten", "emu", "ostrich", "elephant", "sheep", "goat", "fish", "iguana", "monkey", "gorilla", "baboon", "lemming", "mouse", "rat", "hamster", "eagle", "parrot", "crow", "dove", "falcon", "hawk", "sparrow", "kite", "chimpanzee", "giraffe", "bear", "donkey", "mule", "horse", "zebra", "ox", "snake", "cobra", "iguana", "lizard", "fly", "mosquito", "llama", "butterfly", "moth", "worm", "centipede", "unicorn", "pegasus", "griffin", "ogre", "monster", "demon", "witch", "vampire", "mummy", "ghoul", "zombie"), region = c(6l, 4l, 4l, 5l, 7l, 6l, 2l, 4l, 6l, 7l, 7l, 4l, 6l, 4l, 4l, 4l, 4l, 3l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 3l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 8l, 4l, 6l, 6l, 4l, 2l, 7l, 4l, 2l, 2l, 7l, 3l, 4l, 7l, 4l, 4l, 4l, 7l, 7l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 2l, 8l), period = c(-2715, -3500, -3500, -4933.333333, -2715, -2715, -2715, -3500, -2715, -4350, -3500, -3500, -2950, -4350, -3650, -3500, -3500, -2715, -3650, -4350, -3500, -3500, -3400, -4350, -3500, -3500, -4350, -3900, -3808.333333, -4233.333333, -3500, -3900, -3958.333333, -3900, -3500, -3500, -3500, -2715, -3650, -2715, -2715, -2715, -2715, -3500, -2715, -2715, -3500, -4350, -3650, -3650, -4350, -5400, -3500, -3958.333333, -3400, -3400, -4350, -3600, -4350, -3650, -3500, -2715, -5400, -3500), value = c(0.132625995, 0.163120567, 0.228840125, 0.154931973, 0.110047847, 0.054347826, 0.188679245, 0.245014245, 0.128378378, 0.021428571, 0.226277372, 0.176923077, 0.104938272, 0.17659805, 0.143798024, 0.086956522, 0.0625, 0.160714286, 0, 0.235588972, 0, 0, 0.208333333, 0.202247191, 0.364705882, 0.174757282, 0, 0.4, 0.1, 0.184027778, 0.232876712, 0.160493827, 0.74702381, 0.126984127, 0.080645161, 0.06557377, 0, 0.057692308, 0.285714286, 0.489361702, 0.108695652, 0.377777778, 0, 0.522727273, 0.024390244, 0.097560976, 0.275, 0, 0.0625, 0.255319149, 0.135135135, 0.216216216, 0.222222222, 0.296296296, 0.222222222, 0.146341463, 0.09375, 0.125, 0.041666667, 0.078947368, 0.2, 0.137931034, 0.571428571, 0.142857143), sample_size = c(188.5, 105.75, 79.75, 70, 52.25, 46, 39.75, 39, 37, 35, 34.25, 32.5, 32.4, 30.76666667, 30.36666667, 28.75, 28, 28, 28, 26.6, 25, 25, 24, 22.25, 21.25, 20.6, 20, 20, 20, 19.2, 18.25, 18, 18, 16.8, 15.5, 15.25, 15, 13, 12.6, 11.75, 11.5, 11.25, 11, 11, 10.25, 10.25, 10, 10, 9.6, 9.4, 9.25, 9.25, 9, 9, 9, 8.2, 8, 8, 8, 7.6, 7.5, 7.25, 7, 7), sample_sub = c(25, 17.25, 18.25, 10.8452381, 5.75, 2.5, 7.5, 9.555555556, 4.75, 0.75, 7.75, 5.75, 3.4, 5.433333333, 4.366666667, 2.5, 1.75, 4.5, 0, 6.266666667, 0, 0, 5, 4.5, 7.75, 3.6, 0, 8, 2, 3.533333333, 4.25, 2.888888889, 13.44642857, 2.133333333, 1.25, 1, 0, 0.75, 3.6, 5.75, 1.25, 4.25, 0, 5.75, 0.25, 1, 2.75, 0, 0.6, 2.4, 1.25, 2, 2, 2.666666667, 2, 1.2, 0.75, 1, 0.333333333, 0.6, 1.5, 1, 4, 1)), .names = c("site", "region", "period", "value", "sample_size", "sample_sub"), class = "data.frame", row.names = c(na, -64l))
i have been working while , trying read as can, haven't been able make additional headway. advice or guidance appreciated.
follow-up on adding confidence interval plot
i have been trying add in confidence intervals following example found on site on page how confidence intervals lowess fit using r? .
the example given on page is:
plot(cars) plx<-predict(loess(cars$dist ~ cars$speed), se=t) lines(cars$speed,plx$fit) lines(cars$speed,plx$fit - qt(0.975,plx$df)*plx$se, lty=2) lines(cars$speed,plx$fit + qt(0.975,plx$df)*plx$se, lty=2)
i adapted this:
plot(x15p5 ~ period, animals) animals.lo2<-predict(loess(animals$x15p5 ~ animals$period), se=t) <- order(animals$period) lines(animals$period[a],animals.lo2$fit, col="red", lwd=3) lines(animals$period[a],animals.lo2$fit - qt(0.975,animals.lo2$df)*animals.lo2$se, lty=2) lines(animals$period[a],animals.lo2$fit + qt(0.975,animals.lo2$df)*animals.lo2$se, lty=2)
although provide confidence intervals, regression line wrong. i'm not sure if issue predict
function, or issue. again!
correct code
i searched around , read issue due points needing ordered, proceeded.
no, no. ordering issue not related error see. overcome error, need replace
lines(animals$x15p5, animals.lo, col="red")
with
lines(animals$period, animals.lo$fitted, col="red")
here reasons:
loess
returns list of objects, not single vector. seestr(animals.lo)
ornames(animals.lo)
.- why use
animals$x15p5
x-axis? fit model:x15p5 ~ period
, x-axis shouldperiod
.
about reordering
you need ordering, because default, r lines points in order. take example:
set.seed(0); x <- runif(100, 0, 10) ## x not in order set.seed(1); y <- sqrt(x) ## plot curve y = sqrt(x) par(mfrow = c(1,2)) plot(x, y, type = "l") ## mess!! reorder <- order(x) plot(x[reorder], y[reorder], type = "l") ## nice
similarly, do:
a <- order(animals$period) lines(animals$period[a], animals.lo$fitted[a], col="red", lwd=3)
follow-up on confidence interval
try this:
plot(x15p5 ~ period, animals) animals.lo <- loess(x15p5 ~ period, animals) pred <- predict(animals.lo, se = true) <- order(animals$period) lines(animals$period[a], pred$fit[a], col="red", lwd=3) lines(animals$period[a], pred$fit[a] - qt(0.975, pred$df)*pred$se[a],lty=2) lines(animals$period[a], pred$fit[a] - qt(0.975, pred$df)*pred$se[a],lty=2)
you forgot reordering again. need reorder both fitted values, standard errors.
now, dist ~ speed
model cars
data has no need reordering. because:
is.unsorted(cars$speed) ## false
yes, data sorted there.
note have made 2 other changes code:
- i have separated
loess
call ,predict
call; maybe don't need this, habit separate model fitting , model prediction, , keeps copy of both objects. - i have changed
loess(animals$x15p5 ~ animals$period)
loess(x15p5 ~ period, animals)
. bad habit use$
sign in specifying model formula. have answer @ https://stackoverflow.com/a/37307270/4891738 showing draw of such style. can read on "update" section on there. have usedglm
example,lm
,glm
,loess
, things same.
Comments
Post a Comment