0

I just realized that in this post I put together the histogram has just index values on the x-axis, instead of the intended 0 to 5 range:

enter image description here

Here is the code for convenience:

x1 = 5           # Maximum value
x0 = 0.1         # It can't be zero; otherwise X^0^(neg) is 1/0.
alpha = -2.5     # It has to be negative.
y = runif(1e5)   # Number of samples
x = ((x1^(alpha+1) - x0^(alpha+1))*y + x0^(alpha+1))^(1/(alpha+1))

h = hist(x, prob=T, breaks=40, plot=F)
plot(h$count, log="xy", type='l', lwd=1, lend=2, 
xlab="", ylab="", main="Density in logarithmic scale")

Logically, I have search and found some pertinent posts, such as this and this, and have tried things like

x1 = 5           # Maximum value
x0 = 0.1         # It can't be zero; otherwise X^0^(neg) is 1/0.
alpha = -2.5     # It has to be negative.
y = runif(1e5)   # Number of samples
x = ((x1^(alpha+1) - x0^(alpha+1))*y + x0^(alpha+1))^(1/(alpha+1))

h = hist(x, prob=T, breaks=40, plot=F)
plot(h$count, log="xy", type='l', lwd=1, lend=2, 
xlab="", ylab="", xaxt = 'n', main="Density in logarithmic scale")
axis(side=1, at=seq(0, 5, .2), labels=seq(0, 5, .2))

getting non-sensical results:

enter image description here

Antoni Parellada
  • 4,253
  • 6
  • 49
  • 114

1 Answers1

1

You might be looking for density().

plot(density(x), log="y", ylab="log density x", col=2)
legend("topright", "x", lty=1, col=2)

enter image description here

Note: set.seed(42).

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • Thank you. Do you know how to produce a histogram with the same x axis? – Antoni Parellada Oct 26 '19 at 18:44
  • @Toni I guess you mean the x axis should also be logarithmic? – jay.sf Oct 26 '19 at 18:51
  • No, that's fine, `log = "y"` or `log = "xy"`... I was asking about avoiding altogether the density function - although it is the best choice - and doing the same (discretized or binned) with hist(). – Antoni Parellada Oct 26 '19 at 18:53
  • @Toni I'm not sure about the benefits, `hist` calculates densities as well and the `lines` function smooths then over the bins? However, we probably should take more care of the `from`, `to`, `cut`etc. parameters of `density()`. – jay.sf Oct 26 '19 at 18:58