[英]Error with generating sequences when graphing plots in R
Simply trying to generate a function for plotting area under the curve for az score or set of z scores, but when I give two z scores with zshade(c(1,2))
I get the following error: 简单地尝试生成一个函数以绘制曲线下的z分数或z分数集的面积,但是当我用zshade(c(1,2))
给出两个z分数时,会出现以下错误:
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. 但是我不确定为什么会这样,我再次检查了z2
,它的长度确实为1,所以我不确定错误在哪里。
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)) 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")
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.