Sm.density.compare (): display multiple density estimates in a single graph

I am trying to overlay three different density graphs in R to create one graph displaying all three lines (overlay). I installed / downloaded the package sm, but I tried to use it with my data to no avail. I created three separate data plots, simply using density()and plotting the values. My code is as follows:

library(sm)

set.seed(0)
x <- rnorm(100, 0, 1)
y <- rnorm(126, 0.3, 1.2)
z <- rnorm(93, -0.5, 0.7)
dx <- density(x)
dy <- density(y)
dz <- density(z)

plot(dx)
plot(dy)
plot(dz)

But when I try to use sm.density.compare()for charting:

sm.density.compare(dx,dy,model="equal")

I get an error message:

Error in sm.density.compare (dx, dy, model = "equal"):
sm.density.compare can only process 1-d data trace:

- , ? , . R .

+4
1

sm.density.compare(), density().

sm.density.compare() . , , .

:

## three groups, each of length: length(x), length(y), length(z)
group.index <- rep(1:3, c(length(x), length(y), length(z)))
## collect data together and use sm.density.compare()
den <- sm.density.compare(c(x,y,z), group = group.index, model = "equal")
## plot will be generated automatically

den3

model = "equal", sm.density.compare() . str(den):

List of 4
 $ p          : num 0
 $ estimaate  : num [1:3, 1:50] 2.37e-07 3.81e-06 6.06e-10 2.17e-06 2.26e-05 ...
 $ eval.points: num [1:50] -4.12 -3.94 -3.76 -3.58 -3.4 ...
 $ h          : num 0.376

h , , eval.points , estimaate - . ( , "", "", LOL).

sm, sm. ..., sm.options. ?sm.options, , , , ..

. , sm.density.compare() . :

den2 <- sm.density.compare(c(x,y), group = rep(1:2, c(length(x), length(y))),
                           model = "equal")

den2

> str(den2)
List of 6
 $ p          : num 0.22
 $ estimate   : num [1:2, 1:50] 4.92e-06 2.70e-05 2.51e-05 1.00e-04 1.09e-04 ...
 $ eval.points: num [1:50] -4.12 -3.94 -3.76 -3.58 -3.4 ...
 $ upper      : num [1:50] 0.00328 0.00373 0.00459 0.00614 0.00886 ...
 $ lower      : num [1:50] 0 0 0 0 0 ...
 $ h          : num 0.44

lower upper / .


density(), sm.density.compare()

## set universal estimation range
xlim <- range(x, y, z)
dx <- density(x, from = xlim[1], to = xlim[2], n = 200)
dy <- density(y, from = xlim[1], to = xlim[2], n = 200)
dz <- density(z, from = xlim[1], to = xlim[2], n = 200)

. "" , :

> str(dx)
List of 7
 $ x        : num [1:200] -2.64 -2.61 -2.58 -2.55 -2.52 ...
 $ y        : num [1:200] 0.023 0.026 0.0291 0.0323 0.0356 ...
 $ bw       : num 0.31
 $ n        : int 100
 $ call     : language density.default(x = x, n = 200, from = xlim[1], to = xlim[2])
 $ data.name: chr "x"
 $ has.na   : logi FALSE
 - attr(*, "class")= chr "density"

x - , y - , bw - . , dx$bw, dy$bw dz$bw , . density() bw, bw. . ?density, .

, , .

## set global plotting range
ylim <- range(dx$y, dy$y, dz$y)
## make plot
plot(dx$x, dx$y, col = 1, lwd = 2, type = "l", xlim = xlim, ylim = ylim)
lines(dy$x, dy$y, col = 2, lwd = 2)
lines(dz$x, dz$y, col = 3, lwd = 2)

do it yourself

+6

All Articles