Simply trying to generate a function for plotting area under the curve for a z score or set of z scores, but when I give two z scores with zshade(c(1,2))
I get the following error:
Error in seq.default(z1, z2, 0.01) : 'to' must be of length 1
But I'm not sure why this is the case, I double checked z2
and it's indeed of length 1 so I'm unsure where the error is.
zshade = function(z, shade = "left") {
# If more than 2 z scores are given
if (length(z) > 2) {
stop("Error: Too many z scores given!")
}
# If two z scores are given
if (length(z) > 1) {
z1 = min(z)
z2 = max(z)
cord.x = c(z1, seq(z1, z2, 0.01), z2)
cord.y = c(0, dnorm(seq(z1, z2, 0.01)), 0)
curve(dnorm(x, 0, 1), xlim = c(-4, 4), main = "Standard Normal",
ylab = "", xlab = "")
polygon(cord.x, cord.y, col = "skyblue")
}
# If a single z score is given
if (shade == "left") {
z1 = -4
z2 = z
cord.x = c(z1, seq(z1, z2, 0.01), z2)
cord.y = c(0, dnorm(seq(z1, z2, 0.01)), 0)
curve(dnorm(x, 0, 1), xlim = c(-4, 4), main = "Standard Normal Curve",
ylab = "", xlab = "")
polygon(cord.x, cord.y, col = "skyblue")
}
if (shade == "right") {
z1 = z
z2 = 4
cord.x = c(z1, seq(z1, z2, 0.01), z2)
cord.y = c(0, dnorm(seq(z1, z2, 0.01)), 0)
curve(dnorm(x, 0, 1), xlim = c(-4, 4), main = "Standard Normal Curve",
ylab = "", xlab = "")
polygon(cord.x, cord.y, col = "skyblue")
}
}
zshade(c(1,2))
Silly mistake, see code below...
zshade = function(z, shade = "left") {
# If more than 2 z scores are given
if (length(z) > 2) {
stop("Error: Too many z scores given!")
}
# If two z scores are given
if (length(z) > 1) {
z1 = min(z)
z2 = max(z)
cord.x = c(z1, seq(z1, z2, 0.01), z2)
cord.y = c(0, dnorm(seq(z1, z2, 0.01)), 0)
curve(dnorm(x, 0, 1), xlim = c(-4, 4), main = "Standard Normal",
ylab = "", xlab = "")
polygon(cord.x, cord.y, col = "skyblue")
}
if (length(z)==1) {
# If a single z score is given
if (shade == "left") {
z1 = -4
z2 = z
cord.x = c(z1, seq(z1, z2, 0.01), z2)
cord.y = c(0, dnorm(seq(z1, z2, 0.01)), 0)
curve(dnorm(x, 0, 1), xlim = c(-4, 4), main = "Standard Normal Curve",
ylab = "", xlab = "")
polygon(cord.x, cord.y, col = "skyblue")
}
if (shade == "right") {
z1 = z
z2 = 4
cord.x = c(z1, seq(z1, z2, 0.01), z2)
cord.y = c(0, dnorm(seq(z1, z2, 0.01)), 0)
curve(dnorm(x, 0, 1), xlim = c(-4, 4), main = "Standard Normal Curve",
ylab = "", xlab = "")
polygon(cord.x, cord.y, col = "skyblue")
}
}
}