For the sake of learning, I am trying to derive a $\chi^2$ density from the standard normal $Z$ density.
The $\chi^2_k$ is a distribution of the sum of squares of $k$ standard normal variables, where $k$ is the the number of degrees of freedom.
Taking $k=1$ for simplicity (i.e. just one standard normal variable), I have tried the following in R:
library(data.table) DT <- data.table(x = seq(0, 3.5, by=0.01)) #In a range from 0 to 3.5
Standard normal density multiplied by 2 to calculate the density of absolute $Z$ values (i.e. negative + positive); this is equivalent to a $\chi$ density.
DT[, Z_abs := 2*dnorm(x)]
Calculate the squares:
DT[, X_squared := x^2]
And plot the density against the squares:
library(ggplot2) ggplot(DT, aes(X_squared, Z_abs)) + geom_line() + scale_x_continuous(limits=c(0,3.5), breaks=seq(0,3.5,0.5))
However, this is apparently wrong as it differs from the correct $\chi^2$ density given by the built-in
dchisq function (shown in red).
DT[, Chisq := dchisq(X_squared, df=1)] ggplot(DT, aes(X_squared, Z_abs)) + geom_line() + geom_line(aes(y=Chisq), color="red") + scale_x_continuous(limits=c(0,3.5), breaks=seq(0,3.5,0.5)) + scale_y_continuous(limits=c(0,0.8)) + ylab("Density")
Where did I get it wrong ?