I have the following image:
我有以下图片:
And I would like to smooth the red and blue line. But I have no idea how to do it. The red and blue lines respectively represent upper & lower 95% intervals of the black dots. (Notice that I didnt use any regression formula to obtain the 95% intervals) I read about the loess function but when i tried to use it. I get back the same plot. So is there any particular built in R function that will allow me to smooth these 2 lines.
我想平滑红线和蓝线。但我不知道该怎么做。红线和蓝线分别代表黑点的上下95%间隔。 (请注意,我没有使用任何回归公式来获得95%的间隔)我读到了黄土函数,但是当我尝试使用它时。我得到了同样的情节。那么是否有任何特殊的内置R功能,可以让我平滑这两行。
Alternatively, is there a way to obtain a "95% point wise intervals" for this problem ?
或者,有没有办法获得这个问题的“95%点间隔”?
The code is given below:
代码如下:
residual.plot <- function(a,b)
{
log.y1 <- log(a) - b * log(energy)
fitted.y <- exp(log.y1)
diff <- count - fitted.y
#normal approximation
low.interval <- c()
high.interval <- c()
for(i in 1:350)
{
low <- diff[i] - sqrt( exp(log(a) - b * log(energy[i])) )*qnorm(0.975)
high <- diff[i] + sqrt( exp(log(a) - b * log(energy[i])) )*qnorm(0.975)
low.interval <- append(low.interval, low)
high.interval <- append(high.interval, high)
}
par(mfrow = c(1,1))
plot(energy, diff, ylim = c(-10,10), type = "p", pch = 7)
lines(energy, low.interval, type = "p", col = "red", pch = 1)
lines(energy, high.interval, type = "p", col = "blue", pch = 1)
}
1 个解决方案
#1
5
First of all, never ever dare posting code like that again. You commit two mortal sins :
首先,再也不敢发布这样的代码了。你犯了两个致命的罪:
- you grow objects in an iterative loop (tons of problems there)
- you don't use the fact that R works vectorized.
你在一个迭代循环中增长对象(那里有很多问题)
你没有使用R工作矢量化的事实。
This said, the easiest way of doing this is by using lowess
, provided there's no NA values in your data. Your function should be then something like this :
这就是说,最简单的方法是使用lowess,前提是数据中没有NA值。你的功能应该是这样的:
residual.plot <- function(a,b,count,energy)
{
log.y1 <- log(a) - b * log(energy)
fitted.y <- exp(log.y1)
diff <- count - fitted.y
#normal approximation
low <- diff - sqrt( exp(log(a) - b * log(energy)) )*qnorm(0.975)
high <- diff + sqrt( exp(log(a) - b * log(energy)) )*qnorm(0.975)
par(mfrow = c(1,1))
plot(energy, diff, ylim = c(-10,10), type = "p", pch = 7)
lines(lowess(energy, low), type = "p", col = "red", pch = 1)
lines(lowess(energy, high), type = "p", col = "blue", pch = 1)
}
PS: To make a function useful, you shouldn't count on variables from outside the function like for example count
and energy
. Add them as an argument to the function, so you can use the function later on when using a different dataset.
PS:为了使函数有用,你不应该依赖函数外部的变量,例如count和energy。将它们作为参数添加到函数中,以便稍后在使用其他数据集时可以使用该函数。
#1
5
First of all, never ever dare posting code like that again. You commit two mortal sins :
首先,再也不敢发布这样的代码了。你犯了两个致命的罪:
- you grow objects in an iterative loop (tons of problems there)
- you don't use the fact that R works vectorized.
你在一个迭代循环中增长对象(那里有很多问题)
你没有使用R工作矢量化的事实。
This said, the easiest way of doing this is by using lowess
, provided there's no NA values in your data. Your function should be then something like this :
这就是说,最简单的方法是使用lowess,前提是数据中没有NA值。你的功能应该是这样的:
residual.plot <- function(a,b,count,energy)
{
log.y1 <- log(a) - b * log(energy)
fitted.y <- exp(log.y1)
diff <- count - fitted.y
#normal approximation
low <- diff - sqrt( exp(log(a) - b * log(energy)) )*qnorm(0.975)
high <- diff + sqrt( exp(log(a) - b * log(energy)) )*qnorm(0.975)
par(mfrow = c(1,1))
plot(energy, diff, ylim = c(-10,10), type = "p", pch = 7)
lines(lowess(energy, low), type = "p", col = "red", pch = 1)
lines(lowess(energy, high), type = "p", col = "blue", pch = 1)
}
PS: To make a function useful, you shouldn't count on variables from outside the function like for example count
and energy
. Add them as an argument to the function, so you can use the function later on when using a different dataset.
PS:为了使函数有用,你不应该依赖函数外部的变量,例如count和energy。将它们作为参数添加到函数中,以便稍后在使用其他数据集时可以使用该函数。