R_dnorm_dt - RとLinuxと...

RとLinuxと...


R_dnorm_dt

Rの備忘録

こんなコードを紹介した。

x <- 1:20
curve(dnorm, -4, 4, col = 2, lty = 2, lwd = 2)
my.dt <- function(x) dt(x, 19)
curve(my.dt, -4, 4, add = T, col = 5, lty= 3, lwd = 2)
legend(-4, .4, legend= c("正規分布", "t分布"),
       col = c(2,5), lty = c(2,3), lwd = 2)

R_正規分布のプロット図もみてください。

par(mfrow = c(2,2))
#########
my.mean <- 50
my.sd <- 10
my.dnorm <- function(x) dnorm(x, mean = my.mean, sd = my.sd)

plot(my.dnorm, 20, 80,  main = paste("平均=", my.mean , "標準偏差= ", my.sd,
 " の正規分布",sep = ""),ylab = "頻度",  xlab = "平均")
xvals <- seq(my.mean - my.sd * 2, my.mean + my.sd * 2,
   length = 100)
dvals <- my.dnorm(xvals)
polygon(c(xvals, rev(xvals) )  , c(rep(0, 100), 
 rev(dvals)), col = "yellow")
text(my.mean, my.dnorm(my.mean) ,  lab = "95%", cex = 1.8)
text(my.mean - my.sd * 2.1, my.dnorm(my.mean - my.sd * 2) , 
   lab = "2.5%", cex = 1.2, col = "red")
text(my.mean + my.sd * 2.1, my.dnorm(my.mean + my.sd * 2) ,
    lab = "2.5%", cex = 1.2, col = "red")
lines(c(my.mean - my.sd * 2, my.mean + my.sd * 2 ), c(0,0))
lines(c(my.mean,my.mean), c(0,my.dnorm(my.mean)), lty=2, 
    col ="blue", lwd = 3)
#########
#########
my.mean <- 175
my.sd <- 8.8
my.dnorm <- function(x) dnorm(x, mean = my.mean, sd = my.sd)

plot(my.dnorm, my.mean - my.sd * 3, my.mean + my.sd * 3,  
  main = paste("平均=", my.mean , 標準偏差=", my.sd,
  " の正規分布",sep = ""),ylab = "頻度",  xlab = "平均")
xvals <- seq(my.mean - my.sd * 2, my.mean + my.sd * 2, 
   length = 100)
dvals <- my.dnorm(xvals)
polygon(c(xvals, rev(xvals) )  , c(rep(0, 100), 
   rev(dvals)), col = "yellow")
text(my.mean, my.dnorm(my.mean) ,  lab = "95%", cex = 1.8)
text(my.mean - my.sd * 2.1, my.dnorm(my.mean - my.sd * 2) ,
 lab = "2.5%", cex = 1.2, col = "red")
text(my.mean + my.sd * 2.1, my.dnorm(my.mean + my.sd * 2) ,
 lab = "2.5%", cex = 1.2, col = "red")
lines(c(my.mean - my.sd * 2, my.mean + my.sd * 2 ), c(0,0))
lines(c(my.mean,my.mean), c(0,my.dnorm(my.mean)), lty=2, 
   col ="blue", lwd = 3)
#########
### legend(-4.5, .4, legend= c("平均を中心に左右に標準偏差2個"))

#########
my.mean <- 7
my.sd <- 2
my.dnorm <- function(x) dnorm(x, mean = my.mean, sd = my.sd)

plot(my.dnorm, my.mean - my.sd * 3, my.mean + my.sd * 3,  
   main = paste("平均=", my.mean , ", 標準偏差=", my.sd,
   " の正規分布",sep = ""),ylab = "頻度",  xlab = "平均")
xvals <- seq(my.mean - my.sd * 2, my.mean + my.sd * 2, 
   length = 100)
dvals <- my.dnorm(xvals)
polygon(c(xvals, rev(xvals) )  , c(rep(0, 100), 
  rev(dvals)), col = "yellow")
text(my.mean, my.dnorm(my.mean) ,  lab = "95%", cex = 1.8)
text(my.mean - my.sd * 2.1, my.dnorm(my.mean - my.sd * 2),
   lab = "2.5%", cex = 1.2, col = "red")
text(my.mean + my.sd * 2.1, my.dnorm(my.mean + my.sd * 2) ,
 lab = "2.5%", cex = 1.2, col = "red")
lines(c(my.mean - my.sd * 2, my.mean + my.sd * 2 ), c(0,0))
lines(c(my.mean,my.mean), c(0,my.dnorm(my.mean)), lty=2, 
   col ="blue", lwd = 3)
#########
#########
my.mean <- 0
my.sd <- 1
my.dnorm <- function(x) dnorm(x, mean = my.mean, sd = my.sd)

plot(my.dnorm, my.mean - my.sd * 3, my.mean + my.sd * 3,  
   main = paste("平均=", my.mean , ", 標準偏差=", my.sd,
   " の正規分布",sep = ""),ylab = "頻度",  xlab = "平均")
xvals <- seq(my.mean - my.sd * 2, my.mean + my.sd * 2, 
  length = 100)
dvals <- my.dnorm(xvals)
polygon(c(xvals, rev(xvals) )  , c(rep(0, 100), 
   rev(dvals)), col = "yellow")
text(my.mean, my.dnorm(my.mean) ,  lab = "95%", cex = 1.8)
text(my.mean - my.sd * 2.1, my.dnorm(my.mean - my.sd * 2),
  lab = "2.5%", cex = 1.2, col = "red")
text(my.mean + my.sd * 2.1, my.dnorm(my.mean + my.sd * 2),
    lab = "2.5%", cex = 1.2, col = "red")
lines(c(my.mean - my.sd * 2, my.mean + my.sd * 2 ), c(0,0))
lines(c(my.mean,my.mean), c(0,my.dnorm(my.mean)), lty=2, 
   col ="blue", lwd = 3)
#########
Last-modified: 2010-06-13 (日) 16:14:51 (3107d)