Question

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!

Was it helpful?

Solution

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()

OTHER TIPS

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))

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top