inst/book/appA.R

###################################################
### chunk number 1: 
###################################################
#line 6 "appa.Rnw"
options(prompt="R> ")
options(width=80)


###################################################
### chunk number 2: 
###################################################
#line 24 "appa.Rnw"
cat("help me!")


###################################################
### chunk number 3: 
###################################################
#line 78 "appa.Rnw"
x <- 4
x = 4


###################################################
### chunk number 4: 
###################################################
#line 84 "appa.Rnw"
y <- c(2,7,4,1)
y


###################################################
### chunk number 5: 
###################################################
#line 89 "appa.Rnw"
z <- matrix(1:30, 5, 6)
z


###################################################
### chunk number 6: 
###################################################
#line 94 "appa.Rnw"
1:30


###################################################
### chunk number 7: 
###################################################
#line 99 "appa.Rnw"
matrix(,5,6)


###################################################
### chunk number 8: 
###################################################
#line 104 "appa.Rnw"
numeric(4)


###################################################
### chunk number 9: 
###################################################
#line 108 "appa.Rnw"
ls()


###################################################
### chunk number 10: 
###################################################
#line 113 "appa.Rnw"
vector(mode="numeric", 4)


###################################################
### chunk number 11: 
###################################################
#line 117 "appa.Rnw"
w1 <- numeric(0)
w1


###################################################
### chunk number 12: 
###################################################
#line 122 "appa.Rnw"
w2 <- NULL
w2


###################################################
### chunk number 13: 
###################################################
#line 128 "appa.Rnw"
O1 <- 1:4
O1
ls()
assign("O2", 5:8)
ls()
O2


###################################################
### chunk number 14: 
###################################################
#line 139 "appa.Rnw"
data(cars)
mod <- lm(dist~speed, data=cars)
mod


###################################################
### chunk number 15: 
###################################################
#line 145 "appa.Rnw"
str(mod)


###################################################
### chunk number 16: 
###################################################
#line 149 "appa.Rnw"
mod$coefficients
str(mod$coefficients)


###################################################
### chunk number 17: 
###################################################
#line 154 "appa.Rnw"
names(mod$coefficients)


###################################################
### chunk number 18: 
###################################################
#line 158 "appa.Rnw"
names(mod$coefficients) <- c("alpha", "beta")
mod$coefficients


###################################################
### chunk number 19: 
###################################################
#line 163 "appa.Rnw"
z
rownames(z) <- c("a","b","c","d","e")
colnames(z) <- c("A","B","C","D","E","F")
z


###################################################
### chunk number 20: 
###################################################
#line 170 "appa.Rnw"
str(mod$model)


###################################################
### chunk number 21: 
###################################################
#line 174 "appa.Rnw"
str(cars)


###################################################
### chunk number 22: 
###################################################
#line 181 "appa.Rnw"
y
y[2:3]
str(y)
str(y[2:3])


###################################################
### chunk number 23: 
###################################################
#line 188 "appa.Rnw"
z
z[1:2, 5:6]


###################################################
### chunk number 24: 
###################################################
#line 193 "appa.Rnw"
z[1:2, c(1,3,6)]


###################################################
### chunk number 25: 
###################################################
#line 197 "appa.Rnw"
z[1:2, c(6,5,4)]


###################################################
### chunk number 26: 
###################################################
#line 201 "appa.Rnw"
z[c("a","c"), "D"]


###################################################
### chunk number 27: 
###################################################
#line 205 "appa.Rnw"
z["c",]


###################################################
### chunk number 28: 
###################################################
#line 209 "appa.Rnw"
z[c(-1,-3),]


###################################################
### chunk number 29: 
###################################################
#line 215 "appa.Rnw"
a <- mod[1:2]
str(a)


###################################################
### chunk number 30: 
###################################################
#line 220 "appa.Rnw"
str(mod["coefficients"])
str(mod[1])


###################################################
### chunk number 31: 
###################################################
#line 225 "appa.Rnw"
str(mod[[1]])
str(mod[["coefficients"]])
str(mod$coefficients)


###################################################
### chunk number 32: 
###################################################
#line 231 "appa.Rnw"
cars[,1]


###################################################
### chunk number 33: 
###################################################
#line 235 "appa.Rnw"
cars$speed
cars[[1]]


###################################################
### chunk number 34: 
###################################################
#line 240 "appa.Rnw"
str(cars[1])
head(cars[1])


###################################################
### chunk number 35: 
###################################################
#line 245 "appa.Rnw"
cars[1:3,]


###################################################
### chunk number 36: 
###################################################
#line 252 "appa.Rnw"
pi
as.integer(pi)
as.integer("3.14")
as.integer("a")


###################################################
### chunk number 37: 
###################################################
#line 263 "appa.Rnw"
summary(cars)
summary(mod)


###################################################
### chunk number 38: 
###################################################
#line 268 "appa.Rnw"
methods(summary)


###################################################
### chunk number 39: 
###################################################
#line 272 "appa.Rnw"
require(stats4)
set.seed(123)
y <- rnorm(100, mean=1.5)
f <- function(theta=0) -sum(dnorm(x=y, mean=theta,log=TRUE))
fit <- mle(f)
fit


###################################################
### chunk number 40: 
###################################################
#line 281 "appa.Rnw"
str(fit)


###################################################
### chunk number 41: 
###################################################
#line 285 "appa.Rnw"
fit@coef


###################################################
### chunk number 42: 
###################################################
#line 289 "appa.Rnw"
showMethods(summary)


###################################################
### chunk number 43: 
###################################################
#line 295 "appa.Rnw"
g <- function(x, K=110){
 max(x-K, 0)
}


###################################################
### chunk number 44: 
###################################################
#line 302 "appa.Rnw"
g(120)
g(99)
g(115, 120)


###################################################
### chunk number 45: 
###################################################
#line 308 "appa.Rnw"
g(150, 120)
g(K=120, x=150)


###################################################
### chunk number 46: 
###################################################
#line 316 "appa.Rnw"
set.seed(123)
x <- runif(5,90,150)
x
sin(x)


###################################################
### chunk number 47: 
###################################################
#line 323 "appa.Rnw"
g(x)


###################################################
### chunk number 48: 
###################################################
#line 327 "appa.Rnw"
x - 100


###################################################
### chunk number 49: 
###################################################
#line 332 "appa.Rnw"
g1 <- function(x, K=110){
 sapply(x, function(x) max(x-K, 0))
}
x
g1(x)


###################################################
### chunk number 50: 
###################################################
#line 342 "appa.Rnw"
g2<- function(x, K=110){
 n <- length(x)
 val <- numeric(n)
 for(i in 1:n)
  val[i] <-  max(x[i]-K,0)
  val
}


###################################################
### chunk number 51: 
###################################################
#line 352 "appa.Rnw"
g3<- function(x, K=110){
 val <- NULL
 for(u in x)
  val <- c(val, max(u-K,0))
  val
}
g1(x)
g2(x)
g3(x)


###################################################
### chunk number 52: 
###################################################
#line 364 "appa.Rnw"
y <- runif(10000, 90, 150)
system.time(g1(y))
system.time(g2(y))
system.time(g3(y))


###################################################
### chunk number 53: 
###################################################
#line 376 "appa.Rnw"
library(snow)
cl <- makeSOCKcluster(c("localhost","localhost"))


###################################################
### chunk number 54:  eval=FALSE
###################################################
## #line 381 "appa.Rnw"
## makeCluster(2)


###################################################
### chunk number 55:  eval=FALSE
###################################################
## #line 385 "appa.Rnw"
## makeCluster(2,type="SOCK")


###################################################
### chunk number 56: 
###################################################
#line 390 "appa.Rnw"
f <- function(x){
 require(sde)
 sde.sim(model="OU", theta=c(1,1,1), M=x)
}


###################################################
### chunk number 57: 
###################################################
#line 397 "appa.Rnw"
tmp <- parLapply(cl, c(50,50), f)
str(tmp)


###################################################
### chunk number 58: 
###################################################
#line 402 "appa.Rnw"
stopCluster(cl)


###################################################
### chunk number 59: 
###################################################
#line 406 "appa.Rnw"
tmp <- f(100)
str(tmp)


###################################################
### chunk number 60: 
###################################################
#line 412 "appa.Rnw"
cl <- makeSOCKcluster(c("localhost","localhost"))
clusterSetupRNG(cl, seed=rep(123,2))
tmp <- parLapply(cl, c(50,50), f)
stopCluster(cl)


###################################################
### chunk number 61: 
###################################################
#line 422 "appa.Rnw"
require(snowfall)

sfInit(parallel=TRUE, cpus=2)
cl2 <- sfGetCluster()
clusterSetupRNG(cl2, seed=rep(123,2))
tmp <- parLapply( cl2, c(50,50), f ) 
sfStop()
str(tmp)


###################################################
### chunk number 62: 
###################################################
#line 439 "appa.Rnw"
require(foreach)
set.seed(123)
tmp <- foreach(i = rep(50,2)) %dopar% f(i)
str(tmp)
getDoParWorkers()


###################################################
### chunk number 63: 
###################################################
#line 449 "appa.Rnw"
require(snowfall)
sfInit(parallel=TRUE, cpus=2)
cl2 <- sfGetCluster()
clusterSetupRNG(cl2, seed=rep(123,2))


###################################################
### chunk number 64: 
###################################################
#line 456 "appa.Rnw"
require(foreach)
require(doSNOW)
registerDoSNOW(cl2)


###################################################
### chunk number 65: 
###################################################
#line 463 "appa.Rnw"
tmp <- foreach(i = rep(50,2)) %dopar% f(i)
str(tmp)
getDoParWorkers()


###################################################
### chunk number 66: 
###################################################
#line 469 "appa.Rnw"
sfStop()
registerDoSEQ()
getDoParWorkers()


###################################################
### chunk number 67: 
###################################################
#line 475 "appa.Rnw"
tmp <- foreach(i = rep(50,2)) %dopar% f(i)


###################################################
### chunk number 68: 
###################################################
#line 479 "appa.Rnw"
require(doMC)
registerDoMC()
options(cores=2)
tmp <- foreach(i = rep(50,2)) %dopar% f(i)
str(tmp)
getDoParWorkers()
registerDoSEQ()
getDoParWorkers()

Try the opefimor package in your browser

Any scripts or data that you put into this service are public.

opefimor documentation built on May 29, 2017, 9:53 p.m.