Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R图中用曲线下拟合的点填充曲线_R_Plot - Fatal编程技术网

在R图中用曲线下拟合的点填充曲线

在R图中用曲线下拟合的点填充曲线,r,plot,R,Plot,我想知道如何有效地(使用短R代码)用点填充曲线,从而填充曲线下的区域 我尝试了一些没有成功的东西,这是我的R代码: data = rnorm(1000) ## random data points to fill the curve curve(dnorm(x), -4, 4) ## curve to be filled by "data" above points(data) ## plotting the points to fill the curve

我想知道如何有效地(使用短R代码)用点填充曲线,从而填充曲线下的区域

我尝试了一些没有成功的东西,这是我的R代码:

data = rnorm(1000)     ## random data points to fill the curve

curve(dnorm(x), -4, 4) ## curve to be filled by "data" above

points(data)           ## plotting the points to fill the curve
n_points=10000#一个大数字
#将曲线存储在变量中并绘制
cc=曲线(dnorm(x),-4,4,n=n_点)
#生成1000个随机点
p=数据帧(x=序列(-4,4,length.out=n个点),y=规则(n=n个点))
#或者p=data.frame(x=runif(n个点,-4,4),y=rnorm(n个点))
#找出cc$x中最接近p$x的值索引
p$ind=findInterval(p$x,cc$x)
#仅保留曲线中p$y小于cc$y的点
p2=p[p$y>=0&p$y
以下是一种使用插值的方法,以确保打印的点不会超过曲线的高度(不过,如果希望实际点标记不突出曲线上方,则需要将阈值设置为略低于曲线高度):

但我们需要更多的点来确保至少获得
n.target
,因为一旦我们将绘制的点限制在曲线下方,随机变化将导致大约一半的时间少于
n.target
点。因此,我们将添加一个
overview.factor
以生成曲线下比我们需要的更多点,然后我们将随机选择要绘制的点的
n.target
。这是一个函数,它负责一般曲线的整个过程

# Plot a specified number of points under a curve
pts.under.curve = function(data, n.target=1000, excess.factor=1.5) {

  # Area under curve as fraction of area of plot region
  aa = sum(data$y*median(diff(data$x)))/(diff(range(data$x))*max(data$y))

  # Number of random points to generate
  n = excess.factor*ceiling(n.target/aa)

  # Generate n random points in x-range of the data and with y value between
  # zero and the maximum y-value of the curve
  pts = data.frame(x=runif(n,min(data$x),max(data$x)), y=runif(n,0,max(data$y)))

  # Using interpolation, keep only those points whose y-value is less than y(x)
  pts = pts[pts$y < approx(data$x,data$y,xout=pts$x)$y, ]

  # Randomly select only n.target points
  pts = pts[sample(1:nrow(pts), n.target), ]

  # Plot the points
  points(pts, pch=16, col="red", cex=0.7)

}

现在,让我们使用不同的分布来测试它:

# Curve to be filled
c.pts = as.data.frame(curve(df(x, df1=100, df2=20),0,5,n=1001)) 

pts.under.curve(c.pts, n.target=200)

曲线的y轴是密度。您的数据对象只是一个向量。这里没有y轴。@PLapointe,谢谢,你有什么建议吗?@Ryan,我需要曲线下的点而不是polygone?很聪明。我在想,是否有办法确保曲线中的
n
点准确无误。这是一个有趣的问题。我添加了一种方法来获得大致所需的数字。仍然在考虑精确的解决方案。我意识到有一种简单的方法可以获得所需的精确点数,但它仍然依赖于随机生成点,然后将其削减。毫无疑问,有一种更优雅的方法可以做到这一点,但暴力的方法完成了任务。谢谢你回答我的问题更多!但愿我能不止一次投票!
# Area ratio
aa = sum(c.pts$y*median(diff(c.pts$x)))/(diff(c(-4,4))*max(c.pts$y))

# Target number of points under curve
n.target = 1000

# Number of random points to generate
n = ceiling(n.target/aa)
# Plot a specified number of points under a curve
pts.under.curve = function(data, n.target=1000, excess.factor=1.5) {

  # Area under curve as fraction of area of plot region
  aa = sum(data$y*median(diff(data$x)))/(diff(range(data$x))*max(data$y))

  # Number of random points to generate
  n = excess.factor*ceiling(n.target/aa)

  # Generate n random points in x-range of the data and with y value between
  # zero and the maximum y-value of the curve
  pts = data.frame(x=runif(n,min(data$x),max(data$x)), y=runif(n,0,max(data$y)))

  # Using interpolation, keep only those points whose y-value is less than y(x)
  pts = pts[pts$y < approx(data$x,data$y,xout=pts$x)$y, ]

  # Randomly select only n.target points
  pts = pts[sample(1:nrow(pts), n.target), ]

  # Plot the points
  points(pts, pch=16, col="red", cex=0.7)

}
c.pts = as.data.frame(curve(dnorm(x), -4, 4)) 

pts.under.curve(c.pts)
# Curve to be filled
c.pts = as.data.frame(curve(df(x, df1=100, df2=20),0,5,n=1001)) 

pts.under.curve(c.pts, n.target=200)