在 for 循环中索引

时间:2021-05-07 15:57:13

标签: r loops indexing

这里是编程初学者。我正在努力理解 R 中 for 循环的索引 .我正在尝试为 df 4、10 和 20 的 3 个卡方分布生成密度曲线。如何引用这 3 个不同的分布?我的代码如下,索引点中有问号。

感谢您的帮助!

找到了索引,感谢您的帮助。但是为什么图中的黑线不像图例中那样是虚线呢?这不是由代码的 lty = 2 部分指定的吗?

x <- seq(-4, 45, length = 500); x  # 500 values for x ranging from -4 to 45
dist.x <- dnorm(x, 20, 5)  #returns CDF of normal distribution with mean 20 and sd 5

degf <- c(4, 10, 20)  # set  the three degrees of freedom
colors <- c("green", "red", "blue", "black")  #set up the four colors
labels <- c("df = 4", "df = 10", "df = 20", "normal")  #label the plots

plot(x, dist.x, type = "p", lty = 2, lwd = 2, xlab= "x value", ylab = "density", main = "Comparison of Normal and Chi-square Distributions")  #plot normal density curve

for (i in x){
  lines(x, dchisq(x, degf[?]), lty = 1, lwd = 2, col = colors["?"])
}

legend("topright", inset = 0.01, title = "Distributions", labels, lwd = 2,  lty = c(1, 1, 1, 2), col = colors)

3 个答案:

答案 0 :(得分:1)

你就快到了:

x <- seq(-4, 45, length = 500); x  # 500 values for x ranging from -4 to 45
dist.x <- dnorm(x, 20, 5)  #returns CDF of normal distribution with mean 20 and sd 5

degf <- c(4, 10, 20)  # set  the three degrees of freedom
colors <- c("green", "red", "blue", "black")  #set up the four colors
labels <- c("df = 4", "df = 10", "df = 20", "normal")  #label the plots

plot(x, dist.x, type = "p", lty = 2, lwd = 2, xlab= "x value", ylab = "density", main = "Comparison of Normal and Chi-square Distributions")  #plot normal density curve

for (i in seq_along(degf)){
  lines(x, dchisq(x, degf[i]), lty = 1, lwd = 2, col = colors[i])
}

legend("topright", inset = 0.01, title = "Distributions", labels, lwd = 2,  lty = c(1, 1, 1, 2), col = colors)

enter image description here

答案 1 :(得分:1)

一种方法:

x = seq(-4, 45, length = 500)

# put data in data.frame
df <- data.frame(
    ychisq4  = dchisq(x,  4),
    ychisq10 = dchisq(x, 10),
    ychisq20 = dchisq(x, 20),
    ynorm    = dnorm(x, 20, 5)
)

# create empty plot
plot(x = NA, y = NA, 
     xlim = c(-4, 45), 
     ylim = c(0, 0.2),
     xlab = "x value", 
     ylab = "density", 
     main = "Comparison of Normal and Chi-square Distributions")

# specify color and label vectors
mycolors <- c("green", "red", "blue", "black")
mylabels <- c("df = 4", "df = 10", "df = 20", "normal")
mylines <- c(1, 1, 1, 2)

# add legend
legend("topright", 
       inset = 0.01, 
       title = "Distributions", 
       legend = mylabels, 
       lwd = 2,  
       lty = c(1, 1, 1, 2), 
       col = mycolors)

# add lines to plot
for(i in 1:4) {
    lines(x, df[,i], lty = mylines[i], lwd = 2, col = mycolors[i])
}

enter image description here

答案 2 :(得分:0)

给你:

plot(x, dist.x, type = "p", lty = 2, ylim=c(0,0.2), lwd = 2, xlab= "x value", ylab = "density", col = colors[4],
     main = "Comparison of Normal and Chi-square Distributions")  #plot normal density curve

for (i in 1:3){
  lines(x, dchisq(x, degf[i]), lty = 1, lwd = 2, col = colors[i])
}

legend("topright", inset = 0.01, title = "Distributions", labels, lwd = 2,  lty = c(1, 1, 1, 2), col = colors)