LoginSignup
0
0

ベイズ統計モデリング 写経 3.7

Last updated at Posted at 2024-01-09

3.7.3 関数の作成

asqplusb = function(a, b=1)
{
  c = a*2 + b
  return(c)
}
asqplusb(2)
## [1] 5
asqplusb(2,3)
## [1] 7
asqplusb(3,2)
## [1] 8
asqplusb(a=2,b=3)
## [1] 7

3.7.4 条件分岐

x=5
if(x <= 3){
  show("small")
}else{
  show("big")
}
## [1] "big"
for(i in 5:1){
  show(i)
}
## [1] 5
## [1] 4
## [1] 3
## [1] 2
## [1] 1

時間計測

startTime = proc.time()
y=vector(mode="numeric",length=1.0E6)
for( i in 1:1.0E6 ){
  y[i]=log(i)
}
stopTime = proc.time()
elapsedTime = stopTime - startTime
show(elapsedTime)
##    ユーザ   システム       経過  
##       0.58       0.01       0.75

ループを使わないで、ベクトルで

startTime = proc.time()
y=log(1:1.0E6)
stopTime = proc.time()
elapsedTime = stopTime - startTime
show(elapsedTime)
##    ユーザ   システム       経過  
##       0.03       0.00       0.03

forループよりベクトル演算を先に使う。

3.8 グラフの展開と保存

library(ggplot2)
basic <- ggplot( mtcars , aes(x=mpg, y=wt)) + 
  geom_point()
basic

ggsave("test.png")
## Saving 7 x 5 in image
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0