2013年1月26日土曜日

Rで簡単な関数の描画





# housekeeping
rm(list=ls(all=TRUE))   

# Weibull PDF
fw <- function (x) {
    ( k / lambda ) * ( x / lambda ) ^ ( k -1 ) * exp( - ( x / lambda ) ^ k )
}

k <- 1
lambda <- 1
plot ( fw , 0 , 3 , ylim = c( 0 , 1.5 ) , main = "Weibull PDF")

k <- 2
lambda <- 1
par ( new = T )
plot ( fw, 0 , 3 , ylim = c( 0 , 1.5 ) , col = "red" )

k <- 3
lambda <- 1
par ( new = T )
plot ( fw, 0 , 3 , ylim = c( 0 , 1.5 ) , col = "blue" )

k <- 1
lambda <- 2
par ( new = T )
plot ( fw , 0 , 3 , ylim = c( 0 , 1.5 ) , lty = 2 )

k <- 2
lambda <- 2
par ( new = T )
plot ( fw, 0 , 3 , ylim = c( 0 , 1.5 ) , col = "red" , lty = 2 )

k <- 3
lambda <- 2
par ( new = T )
plot ( fw, 0 , 3 , ylim = c( 0 , 1.5 ) , col = "blue" , lty = 2 )

labels <- c( "k=1, lambda=1", "k=2, lambda=1", "k=3, lambda=1", "k=1, lambda=2", "k=2, lambda=2" , "k=3, lambda=2")
cols <- c( "black", "red", "blue", "black", "red", "blue")
ltys <- c( 1, 1, 1, 2, 2, 2)

legend( 2 , 1.5 , legend = labels , col = cols, lty = ltys )

dev.copy2eps(file = "WeibullPDF.eps", width = 8)
dev.copy2pdf(file = "WeibullPDF.pdf", width = 8)

# Weibull CDF
Fw <- function (x) {
    1 - exp( - ( x / lambda ) ^ k )
}

dev.set (1)

k <- 1
lambda <- 1
plot ( Fw , 0 , 3 , ylim = c( 0 , 1 ) , main = "Weibull CDF")

k <- 2
lambda <- 1
par ( new = T )
plot ( Fw, 0 , 3 , ylim = c( 0 , 1 ) , col = "red" )

k <- 3
lambda <- 1
par ( new = T )
plot ( Fw, 0 , 3 , ylim = c( 0 , 1 ) , col = "blue" )

k <- 1
lambda <- 2
par ( new = T )
plot ( Fw , 0 , 3 , ylim = c( 0 , 1 ) , lty = 2 )

k <- 2
lambda <- 2
par ( new = T )
plot ( Fw, 0 , 3 , ylim = c( 0 , 1 ) , col = "red" , lty = 2 )

k <- 3
lambda <- 2
par ( new = T )
plot ( Fw, 0 , 3 , ylim = c( 0 , 1 ) , col = "blue" , lty = 2 )

legend( 2 , 0.3 , legend = labels , col = cols, lty = ltys )

dev.copy2eps(file = "WeibullCDF.eps", width = 8)
dev.copy2pdf(file = "WeibullCDF.pdf", width = 8)
参考
http://r.livedocs.net/graph/legend.html
http://en.wikipedia.org/wiki/Weibull_distribution

0 件のコメント:

コメントを投稿