Hi,
In the following code (which is about histogram), what does the line in Italic (*) mean?
x <- mtcars$mpg
h<-hist(x,
breaks=12,
col="red",
xlab="Miles Per Gallon",
main="Histogram with normal curve and box")
xfit<-seq(min(x), max(x), length=40)
yfit<-dnorm(xfit, mean=mean(x), sd=sd(x))
*yfit <- yfit*diff(h$mids[1:2])length(x)
lines(xfit, yfit, col="blue", lwd=2)
It reduces to a vector equal to yfit * 32. It's not obvious what purpose the calculation serves.
(BTW, this is simple enough not to require one, but a reproducible example, called a reprex is always a good idea.)
Are you sure? I checked, it is yfit*64 ((13-11)*32). 32 = length x. 11, 13 = h$mids[1:2]
Do you know what mids[1:2] mean?
x <- mtcars$mpg
xfit<-seq(min(x), max(x), length=40)
yfit<-dnorm(xfit, mean=mean(x), sd=sd(x))
a <- yfit
b <- h$mids[1:2]
c <- length(x)
d <- diff(b)
e <- c * d
f <- e * a
f == yfit*diff(h$mids[1:2])*length(x)
#> [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#> [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#> [31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
Created on 2020-01-15 by the reprex package (v0.3.0)
str(h)
will show you where h$mids come from
system
Closed
5
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.