Dave Jarvis
Dave Jarvis

Reputation: 31161

Random, curvy distribution of data points

Background

Provide an example of R programming.

Problem

Create a distribution of values that, when modeled, produces a curve that resembles:

Essentially, I would like to do something like:

x <- seq( 0, 2, by=0.01 )
y <- sin( 2 * pi * cos( x - 1/2 ) )
plot( x, y * runif( x ) )

But without the clump of data points around 0.5:

Question

How would you create such a distribution?

Thank you!

Upvotes: 3

Views: 340

Answers (3)

bill_080
bill_080

Reputation: 4750

slo<-0.5 #slope of underlying trend
sta<--0.5 #starting y value
amp<-0.2 #amplitude of sine wave
fre<-3 #frequency of sine wave
noi<-0.8 #amplitude of noise term
x<-seq(0,2,0.01)
y<-sta+(slo*x)+(amp*sin(fre*x)) #y no noise
ywnoise<-y+(noi*(runif(length(x))-0.5)) #y with noise

plot(x,ywnoise)
lines(x,y, col="orange")
grid()

Upvotes: 3

IRTFM
IRTFM

Reputation: 263301

Since sin(2*pi*cos(x-0.5)) goes to zero at 0.5 you should try just adding runif()

x <- seq( 0, 2, by=0.01 )
y <- sin( 2 * pi * cos( x - 1/2 ) ) +runif(201)
plot( x,y  )
lines(loess(y~x)$x, lowess(y~x)$y)

Upvotes: 1

nico
nico

Reputation: 51640

Hmmm... I'm not sure if you need any specific statistical property for your distribution, but something like this gets rid of the clump

plot(x,y+rnorm(length(x), 0, 0.2))

Upvotes: 1

Related Questions