### R code from vignette source 'Bart12f.Rnw'
### Encoding: ISO8859-1

###################################################
### code chunk number 1: Bart12f.Rnw:149-151
###################################################
mygd <- function(name, width, height, ...)
  pdf(file = paste(name, "pdf", sep = "."), width = width, height = height, family="URWHelvetica",useDingbats =F, ...)


###################################################
### code chunk number 2: thomas.Rnw:183-184
###################################################
data(iris)  


###################################################
### code chunk number 3: thomas.Rnw:189-190
###################################################
iris[1:3,] 


###################################################
### code chunk number 4: thomas.Rnw:201-203
###################################################
options(width=70)
summary(iris)  


###################################################
### code chunk number 5: thomas.Rnw:213-214
###################################################
pairs(iris[,1:4], col=c("red", "green",  "blue")[as.numeric(iris$Species)])


###################################################
### code chunk number 6: thomas.Rnw:229-231
###################################################
2+2
5*3*4


###################################################
### code chunk number 7: thomas.Rnw:234-236
###################################################
year <- c(1800,1850,1900,1950,2000)
pop <- c(18,54,500,1701,7731)


###################################################
### code chunk number 8: thomas.Rnw:244-245
###################################################
print( c(year,pop))


###################################################
### code chunk number 9: popyear1
###################################################
plot(pop~year, pch=15)


###################################################
### code chunk number 10: thomas.Rnw:250-251
###################################################
plot(pop~year, pch=15)


###################################################
### code chunk number 11: thomas.Rnw:260-261
###################################################
getwd()	


###################################################
### code chunk number 12: thomas.Rnw:264-265
###################################################
ls()


###################################################
### code chunk number 13: thomas.Rnw:351-355
###################################################
x <- c(1,2)
y <- c(3,4)
z <- c(x,y)
x ==y 


###################################################
### code chunk number 14: thomas.Rnw:369-370
###################################################
rep(1,10)


###################################################
### code chunk number 15: thomas.Rnw:374-377
###################################################
v <- c(1,2,4)
letters <- c("a","b","c")
rep(letters, v)


###################################################
### code chunk number 16: thomas.Rnw:405-406
###################################################
seq(from=5, to=22, by=3)	


###################################################
### code chunk number 17: thomas.Rnw:409-410
###################################################
seq(5,22,3)


###################################################
### code chunk number 18: thomas.Rnw:413-414
###################################################
seq(0,10) == 0:10


###################################################
### code chunk number 19: thomas.Rnw:417-418
###################################################
c("one","two","three")


###################################################
### code chunk number 20: thomas.Rnw:428-431
###################################################
x <- c(1,2,3)
y <- c(1,2,4)
x+y


###################################################
### code chunk number 21: thomas.Rnw:435-436
###################################################
x < y


###################################################
### code chunk number 22: thomas.Rnw:440-442
###################################################
y <- c(5,6)
x+y


###################################################
### code chunk number 23: thomas.Rnw:454-455
###################################################
x<- c(2,3,5,7)


###################################################
### code chunk number 24: thomas.Rnw:462-463
###################################################
sum(x)/length(x)


###################################################
### code chunk number 25: thomas.Rnw:475-476
###################################################
xbar <- sum(x)/length(x)


###################################################
### code chunk number 26: thomas.Rnw:478-479
###################################################
sqrt( sum(  (x - xbar)^2 / (length(x)-1) ) )


###################################################
### code chunk number 27: thomas.Rnw:482-484
###################################################
mean(x)
sd(x)


###################################################
### code chunk number 28: thomas.Rnw:492-494
###################################################
v <- c(10,20,30,40,50,60,70,80,90,100)
v[3]


###################################################
### code chunk number 29: thomas.Rnw:498-499
###################################################
v[ c(3,4,5)]


###################################################
### code chunk number 30: thomas.Rnw:503-504
###################################################
v[-c(2,3)]


###################################################
### code chunk number 31: thomas.Rnw:515-517
###################################################
v[1] <- -10
v


###################################################
### code chunk number 32: thomas.Rnw:523-525
###################################################
v>50
v[v>50]


###################################################
### code chunk number 33: thomas.Rnw:533-534
###################################################
v[ v > 55 & v < 79]


###################################################
### code chunk number 34: thomas.Rnw:542-544
###################################################
y <- c(1, NA, 3, 0, NA)
y


###################################################
### code chunk number 35: thomas.Rnw:547-549
###################################################
y[y==NA] <- 0	
print(y)


###################################################
### code chunk number 36: thomas.Rnw:552-554
###################################################
y[is.na(y)] <- 0
print(y)


###################################################
### code chunk number 37: thomas.Rnw:564-565
###################################################
gender <- c(rep("female", 3), rep("male", 5))


###################################################
### code chunk number 38: thomas.Rnw:568-569
###################################################
gender <- factor(gender)	


###################################################
### code chunk number 39: thomas.Rnw:573-575
###################################################
gender
as.numeric(gender)


###################################################
### code chunk number 40: thomas.Rnw:579-580
###################################################
levels(gender)	


###################################################
### code chunk number 41: thomas.Rnw:588-591
###################################################
x <- 1:20
dim(x) <- c(5,4)
x


###################################################
### code chunk number 42: thomas.Rnw:594-595
###################################################
matrix(1:20, nrow= 5)


###################################################
### code chunk number 43: thomas.Rnw:605-609
###################################################
A <- matrix(1:20, nrow= 5, byrow=T)
rownames(A) <- LETTERS[1:nrow(A)]
colnames(A) <- 1:ncol(A)
A


###################################################
### code chunk number 44: thomas.Rnw:618-622
###################################################
A <- matrix(1:4, nrow= 2, byrow=T)
B <- matrix(10*(1:4), nrow= 2, byrow=T)
cbind(A,B)
rbind(A,B)


###################################################
### code chunk number 45: thomas.Rnw:632-634
###################################################
l <- list(  c("a", "b", "c"), 1:4, c(TRUE, TRUE, FALSE))
l


###################################################
### code chunk number 46: thomas.Rnw:643-644
###################################################
l[1]


###################################################
### code chunk number 47: thomas.Rnw:647-648
###################################################
l[2:3]


###################################################
### code chunk number 48: thomas.Rnw:651-653
###################################################
l[[1]]
l[[1]][2]


###################################################
### code chunk number 49: thomas.Rnw:665-666
###################################################
l


###################################################
### code chunk number 50: thomas.Rnw:671-673
###################################################
l[[1]]   <- NULL
l


###################################################
### code chunk number 51: thomas.Rnw:676-679
###################################################
l <-  c(l,l)
l <- l[-c(3,4)]
l


###################################################
### code chunk number 52: thomas.Rnw:690-691
###################################################
length(l)


###################################################
### code chunk number 53: thomas.Rnw:694-696
###################################################
l[[length(l)+1]] <- c("x","y")
l


###################################################
### code chunk number 54: thomas.Rnw:704-706
###################################################
names(l) <- c("numbers", "booleans")
l


###################################################
### code chunk number 55: thomas.Rnw:739-743
###################################################
year <- c(1800,1850,1900,1950,2000)
pop <- c(18,54,500,1701,7731)
demography <- data.frame(y=year, p =pop)
demography


###################################################
### code chunk number 56: thomas.Rnw:814-818
###################################################
cars <- list(speed=c(180, 250, 300), 
price = c(10.5, 55.6, 76.0), 
consumption=c(5, 7.1, 12.5))
cars


###################################################
### code chunk number 57: thomas.Rnw:823-824
###################################################
lapply(cars, sum)


###################################################
### code chunk number 58: thomas.Rnw:853-854
###################################################
lapply(cars, function(x) return(x[2]))


###################################################
### code chunk number 59: thomas.Rnw:886-887
###################################################
unlist(lapply(cars, mean))


###################################################
### code chunk number 60: thomas.Rnw:890-891
###################################################
sapply(cars, mean)


###################################################
### code chunk number 61: thomas.Rnw:894-896
###################################################
data(iris)
sapply(iris, mean)	


###################################################
### code chunk number 62: thomas.Rnw:906-907
###################################################
sort(iris$Sepal.Length)[1:10]


###################################################
### code chunk number 63: thomas.Rnw:910-912
###################################################
a <- c(20,-1,4,3)
order(a)


###################################################
### code chunk number 64: thomas.Rnw:921-925
###################################################
a
i <- order(a)
i
a[i]


###################################################
### code chunk number 65: thomas.Rnw:928-931
###################################################
i <- order(iris$Sepal.Length)
options(width=70)
iris[i,][1:4,]


###################################################
### code chunk number 66: thomas.Rnw:969-973
###################################################
celsius<-(0:4)*10
fahrenheit <- 9/5*celsius+32
conversion <- data.frame(c=celsius, f=fahrenheit)
print(conversion)


###################################################
### code chunk number 67: thomas.Rnw:984-987
###################################################
require(stats)
x <- 1:1000000
system.time(y <- x^2)


###################################################
### code chunk number 68: thomas.Rnw:990-994
###################################################
x <- 1:1000000
system.time(
for( i in 1:length(x)) y[i] <- x[i]^2
)


###################################################
### code chunk number 69: thomas.Rnw:1004-1005
###################################################
for( i in 1:5) print(i)	


###################################################
### code chunk number 70: thomas.Rnw:1009-1014
###################################################
x<-1
while(x <= 5){
print(x)
x <- x+1
}


###################################################
### code chunk number 71: thomas.Rnw:1023-1029
###################################################
x <- 1
repeat{
print(x)
x<-x+1
if (x > 5) break
}


###################################################
### code chunk number 72: thomas.Rnw:1037-1041
###################################################
year <- c(1800,1850,1900,1950,2000)
pop <- c(18,54,500,1701,7731)
range(year)
summary(year)


###################################################
### code chunk number 73: plotxy
###################################################
plot(year, pop)   


###################################################
### code chunk number 74: plotyx
###################################################
plot(y=year, x=pop)   


###################################################
### code chunk number 75: thomas.Rnw:1089-1091
###################################################
norm <- function(x) sqrt(x%*%x)
norm(1:4)


###################################################
### code chunk number 76: thomas.Rnw:1094-1098
###################################################
h <- function(x){ 
	if (x<0)  -1
	else 1}
h(1)


###################################################
### code chunk number 77: thomas.Rnw:1101-1103
###################################################
heaviside <- function(x) ifelse(x<0,-1,1)
heaviside(1)


###################################################
### code chunk number 78: sin1
###################################################
x <- (0:20)*pi/10
y <- sin(x)
plot(y~x)	


###################################################
### code chunk number 79: lines
###################################################
n<-100
x <- (0:n)*2*pi/100
y <- sin(x)+rnorm(n+1)
plot(y~x)	
lines(x,sin(x))


###################################################
### code chunk number 80: sin2
###################################################
plot( (1:50)*0.92, sin( (1:50)*0.92))


###################################################
### code chunk number 81: sin3
###################################################
par(mfrow=c(3,1))
plot( (1:50)*0.92, sin( (1:50)*0.92))
plot( (1:50)*0.92, cos( (1:50)*0.92))
plot( (1:50)*0.92, tan( (1:50)*0.92))


###################################################
### code chunk number 82: thomas.Rnw:1224-1226
###################################################
df.simple <- read.table("simple.txt", header = TRUE)
df.simple


###################################################
### code chunk number 83: irishist
###################################################
data(iris)
x <- iris$Sepal.Length
dens <- density(x)
hist(x,freq=F)
lines(dens)


###################################################
### code chunk number 84: thomas.Rnw:1298-1299
###################################################
stem(iris$Sepal.Length)


###################################################
### code chunk number 85: boxfos
###################################################
boxplot(iris$Sepal.Length)


###################################################
### code chunk number 86: irissepal
###################################################
plot(Sepal.Length~Petal.Length,data=iris)


###################################################
### code chunk number 87: thomasDoe.Rnw:271-289
###################################################
set.seed(123)
library(SPOT)
fn <- spotBraninFunction #test function to be optimized by SANN
x0 <- c(-2,3) #starting point that SANN uses when optimizing Branin
maxit <- 100 #number of evaluations of Branin allowed for SANN
temp <- 10
tmax <- 10
n <- 100
y <- rep(1,n)
y0<-sapply(y, function(x) x<-optim(par=x0, fn=fn, method="SANN"
                                   , control=list(maxit=maxit,
                                                  temp=temp, tmax=tmax))$value)
temp <- 4
tmax <- 62
y <- rep(1,n)
y1<-sapply(y, function(x) x<-optim(par=x0, fn=fn, method="SANN"
                                   , control=list(maxit=maxit,
                                                  temp=temp, tmax=tmax))$value)


###################################################
### code chunk number 88: thomasDoe.Rnw:297-299
###################################################
summary(y0)
summary(y1)


###################################################
### code chunk number 89: thomasDoe.Rnw:301-302
###################################################
boxplot(y0,y1)


###################################################
### code chunk number 90: thomasDoe.Rnw:309-313
###################################################
par(mfrow=c(2,1))
hist(y0,xlim = c( min(y0,y1), max(y0,y1)))
hist(y1,xlim = c( min(y0,y1), max(y0,y1)))
par(mfrow=c(1,1))


###################################################
### code chunk number 91: thomasDoe.Rnw:321-324
###################################################
df1 <- read.table("Data.d/NULL.res",header=T)
options(width=80)
df1[1:10,]


###################################################
### code chunk number 92: thomasDoe.Rnw:332-335
###################################################
library(car)
scatterplotMatrix(~VARX1+VARX2+Y, reg.line=lm, smooth=TRUE,
 spread=FALSE, span=0.5, diagonal = 'density', data=df1)


###################################################
### code chunk number 93: martin.Rnw:42-43 (eval = FALSE)
###################################################
## install.packages("SPOT")


###################################################
### code chunk number 94: martin.Rnw:47-48 (eval = FALSE)
###################################################
## require("SPOT")


###################################################
### code chunk number 95: martin.Rnw:51-53 (eval = FALSE)
###################################################
## ?spot
## ?spotOptim


###################################################
### code chunk number 96: martin.Rnw:56-57 (eval = FALSE)
###################################################
## demo(package="SPOT")


###################################################
### code chunk number 97: martin.Rnw:60-61 (eval = FALSE)
###################################################
## demo("spotDemo18ForresterOptim",ask=F)


###################################################
### code chunk number 98: martin.Rnw:64-65 (eval = FALSE)
###################################################
## spotGui()


###################################################
### code chunk number 99: martin.Rnw:92-97
###################################################
#Find minimum of 2D-sphere function with SANN
fn<-function(x){return(sum(x^2))}
result<-optim(par=c(2,-4),fn,method="SANN")
result$value
result$par


###################################################
### code chunk number 100: martin.Rnw:109-110
###################################################
require(SPOT)


###################################################
### code chunk number 101: martin.Rnw:112-115
###################################################
fn <- spotBraninFunction #test function to be optimized by SANN
x0 <- c(-2,3) #starting point that SANN uses when optimizing Branin
maxit <- 100 #number of evaluations of Branin allowed for SANN


###################################################
### code chunk number 102: martin.Rnw:117-123
###################################################
testalgorithm <- function(pars,x0,fn,maxit){
	temp<-pars[1]
	tmax<-pars[2]
	y <- optim(x0, fn, method="SANN",control=list(maxit=maxit,temp=temp,tmax=tmax))
	return(y$value)
}


###################################################
### code chunk number 103: martin.Rnw:135-151 (eval = FALSE)
###################################################
## target <- function(x,y,x0,fn,maxit){
## 	zz<-matrix(0,length(x))
## 	repeats=1000
## 	for(i in 1:repeats){
## 		set.seed(i)
## 		zz =zz + apply(cbind(x,y),1,testalgorithm,x0=x0,fn=fn,maxit=maxit) 
## 	}
## 	return(zz/repeats)
## }
## x <- seq(1, 100, length.out = 100)  
## y <- x  
## z <- outer(x, y, target,x0=x0,fn=fn,maxit=maxit) 
## filled.contour(x, y, z, color.palette=terrain.colors,xlab="temp",ylab="tmax")
## pal <- topo.colors(100)
## require(rgl)
## persp3d(x,y,z,col=pal[cut(z,100)],xlab="TEMP",ylab="TMAX")


###################################################
### code chunk number 104: martin.Rnw:169-181
###################################################
roi<-spotROI(c(1,1),c(100,100),type=c("INT","INT"))
config<-list(alg.func=testalgorithm,
			alg.roi=roi,
			init.design.size=20,
			seq.predictionModel.func="spotPredictForrester",
			seq.predictionOpt.func="spotPredictOptMulti",
			seq.predictionOpt.method="cmaes",	
			seq.predictionOpt.budget=1000,
			report.func="spotReportSens",
			spot.fileMode=T,
			io.verbosity=3,
			auto.loop.nevals=100)


###################################################
### code chunk number 105: martin.Rnw:193-194 (eval = FALSE)
###################################################
## res<-spot(spotConfig=config,x0=x0,fn=fn,maxit=maxit)


###################################################
### code chunk number 106: martin.Rnw:258-263 (eval = FALSE)
###################################################
## spot(spotConfig=append(list(
## 	report.func="spotReportContour",
## 	report.interactive=F),
## 	res),
## 	spotTask="rep")


###################################################
### code chunk number 107: martin.Rnw:266-271 (eval = FALSE)
###################################################
## spot(spotConfig=append(list(
## 	report.func="spotReport3d",
## 	report.interactive=F),
## 	res),
## 	spotTask="rep")


