Issues using optFederov of AlgDesign package - r

i have some issues using the package AlgDesign. I want to create a design using the federov exchange algorithm. Unfortunalty, I run into an confusing error:
Error in apply(data[, numericColumn], 2, mean) :
dim(X) must have a positive length
The error occures using different orders of the grid variables
cand.list = expand.grid(x1 = scale(as.data.frame(c(0, 0.1, 0.2, 0.3, 0.4, 0.5)), center = 0.0, scale = 0.5),
x2 = c("PMX", "MOC","OC","OX2","POS","CX","UX"),
x4 = c("Swap","Invert","Memetic 2-opt","Memetic k-opt","Memetic VNS"),
x5 = c("A","B")
)
federovDesign<-optFederov(~x1*x2*x5*x4,data = cand.list,nullify = 1,nRepeats = 40,center=TRUE)
This codes produces the error stated above. If i use the following code, everything work fine.
cand.list = expand.grid(x1 = scale(as.data.frame(c(0, 0.1, 0.2, 0.3, 0.4, 0.5)), center = 0.0, scale = 0.5),
x2 = c("PMX", "MOC","OC","OX2","POS","CX","UX"),
x3 = c(50, 100,150,200),
x4 = c("Swap","Invert","Memetic 2-opt","Memetic k-opt","Memetic VNS"),
x5 = c("A","B")
)
federovDesign<-optFederov(~x1*x2*x5*x4,data = cand.list,nullify = 1,nRepeats = 40,center=TRUE)
I just add another variable. However, the other variables remain unchanged but the error disappears. I observe this strange behavior every time I am using the package. With a little luck I may get it to work, trying different variable orders within my grid, however, I dont understand the underlaying concept.
I looked at the origin code of the function at github https://github.com/jvbraun/AlgDesign/blob/master/R/FederovOpt.R and tried my first cand.list and no error occures:
cand.list = expand.grid(x1 = scale(as.data.frame(c(0, 0.1, 0.2, 0.3, 0.4, 0.5)), center = 0.0, scale = 0.5),
x2 = c("PMX", "MOC","OC","OX2","POS","CX","UX"),
# x3 = c(50, 100,150,200),
x4 = c("Swap","Invert","Memetic 2-opt","Memetic k-opt","Memetic VNS"),
x5 = c("A","B")
)
data = cand.list
frml<-~x1*x2*x4*x5
if (!exists(".Random.seed"))
set.seed(555111666)
seed<-.Random.seed
if (missing(frml) || !inherits(frml,c("formula","character"))) {
if (missing(data))
stop("frml and data cannot both be missing.")
frml<-~.
}
if (missing(data)) {
# Create a data matrix from the global variables in frml
frmla<-formula(paste("~-1+",paste(all.vars(frml),sep="",collapse="+"),sep=""))
data<-data.frame(model.matrix(frmla,data))
}else {
if (!inherits(data,"data.frame")) {
# to insure the columns are named
data<-data.frame(data)
if (ncol(data)==1)
colnames(data)<-"X1"
}
}
numericColumn<-sapply(data,is.numeric)
frml<-expand.formula(frml,colnames(data),numerics=numericColumn)
X<-model.matrix(frml,data)
model.matrix.default(frml,data)
means<-apply(data[,numericColumn,drop=FALSE],2,mean)
data[,numericColumn]<-sweep(data[,numericColumn,drop=FALSE],2,means)
frml<-expand.formula(frml,colnames(data),numerics=numericColumn)
X<-model.matrix(frml,data)
N <- nrow(X)
k <- ncol(X)
nRound<-0
nTrials<-k+5
if (nTrials<k)
stop("nTrials must be greater than or equal to the number of columns in expanded X")
nTrials<-as.integer(nTrials) # to be safe
rows<-rep(0,nTrials)
nullify = 1
crit<-0
evaluateI<-FALSE
doSpace=NULL
B<-NULL
RandomStart<-FALSE # this has no effect when approximate!=FALSE since nullify is
augment<-FALSE
approximate=FALSE
proportions<-NULL
maxIteration<-1000
nRepeats<-40
DFrac<-1
CFrac<-1
value<-.Call("FederovOpt", X,as.integer(RandomStart),as.integer(rows),as.integer(nullify),
as.integer(crit),as.integer(evaluateI),as.integer(doSpace),B,as.integer(augment),as.integer(approximate),
as.double(proportions),as.integer(nTrials),as.integer(maxIteration),as.integer(nRepeats),
as.double(DFrac),as.double(CFrac),PACKAGE="AlgDesign")
data[,numericColumn]<-sweep(data[,numericColumn,drop=FALSE],2,-means)
RowNos<-sort(1+((value$rows[1:nTrials])%%N))
Design<-data[RowNos,,drop=FALSE]
So whats the matter? What do i miss?

Thank you for your effort. I have found a solution, its a bug:
https://github.com/jvbraun/AlgDesign/issues/3
solved close

Related

Network analysis in R: error using findGraph function in qgraph package

I can't get the findGraph function in the qgraph package to work.
The code I'm using is:
optGraph <- findGraph(dataCors, nrow(data), type = "pcor")
optimalGraph <- qgraph(optGraph, layout = corGraph$layout,
groups = Groups, legend.cex = 0.3,
cut = 0.1, maximum = 1, minimum = 0, esize = 20,
vsize = 5)
With the output:
Error in findGraph(dataCors, nrow(data), type = "pcor") : could not find function "findGraph"
I have downloaded all the recommended packages.

Error while running WTC (Wavelet Coherence) Codes in R

I am doing Wavelet Analysis in R using Biwavelet. However, I receive the error message:
Error in check.datum(y) :
The step size must be constant (see approx function to interpolate)
When I run the following code:
wtc.AB = wtc(t1, t2, nrands = nrands)
Please share your help here. Complete Code is:
# Import your data
Data <- read.csv("https://dl.dropboxusercontent.com/u/18255955/Tutorials/Commodities.csv")
# Attach your data so that you can access variables directly using their
# names
attach(Data)
# Define two sets of variables with time stamps
t1 = cbind(DATE, ISLX)
t2 = cbind(DATE, GOLD)
# Specify the number of iterations. The more, the better (>1000). For the
# purpose of this tutorial, we just set it = 10
nrands = 10
wtc.AB = wtc(t1, t2, nrands = nrands)
# Plotting a graph
par(oma = c(0, 0, 0, 1), mar = c(5, 4, 5, 5) + 0.1)
plot(wtc.AB, plot.phase = TRUE, lty.coi = 1, col.coi = "grey", lwd.coi = 2,
lwd.sig = 2, arrow.lwd = 0.03, arrow.len = 0.12, ylab = "Scale", xlab = "Period",
plot.cb = TRUE, main = "Wavelet Coherence: A vs B")```

Unused arguments at function R

I'm new in R.
Trying to use the rtriangle function.
swimmerToTornado <- trajectory("to tornado")%>%
log_("Going down TORNADO") %>%
addService(sname = "Tornado", timeDist = function() rtriangle( min = 0.1, max = 0.5, mode = 0.5) )
But I get this Error -
Error in rtriangle(1, min = 0.1, max = 0.5, mode = 0.5) :
unused arguments (min = 0.1, max = 0.5, mode = 0.5)
I tried to remove the 1 argument - still the same error.
I found the answer.
swimmerToTornado <- trajectory("to tornado")%>%
log_("Going down TORNADO") %>%
addService(sname = "Tornado", timeDist = function() rtriangle( 1,0.1, 0.5, 0.5))
Or just like the comment above by #jogo, doing a= , b=, c= will work too.

R upper and lower bounds for a regression function

I am trying to bound the following variables in a function
lower = c(Hyp.b = 0.01, Hyp.Di = .0000001),
upper = c(Hyp.b = 1.01, Hyp.Di = .1)
The script is as follows:
Hyp.q.forward.fun = function( time, Hyp.qi, Hyp.b, Hyp.Di ){ # time in days
Hyp.q.theo = Hyp.qi*(1 + Hyp.b*Hyp.Di*time)^(-1/Hyp.b)
return(Hyp.q.theo)
}
residfun = function(x,x.days,y.prod){
Hyp.qi = x[1]
Hyp.b = x[2]
Hyp.Di = x[3]
q.theo = (365.25/12)*Hyp.q.forward.fun(
time=x.days,
Hyp.qi=Hyp.qi,
Hyp.b=Hyp.b,
Hyp.Di=Hyp.Di)
#plot(x.days,y.prod); lines(x.days,q.theo)
residual = sqrt(sum((q.theo-y.prod)^2))
return(residual)
}
I'm not sure if I'm even using the correct method to bound the two variable. Any help would be much appreciated.

How can I code a self arrow in my node diagram in R?

The code I found creates a population matrix node diagram. All I want to do is add a self arrow on the final node i.e. one that points to itself. Hope you can help.
library(diagram)
Numgenerations <- 6
DiffMat <- matrix(data = 0, nrow = Numgenerations, ncol = Numgenerations)
AA <- as.data.frame(DiffMat)
AA[[1,5]] <- "f[4]"
AA[[1,6]] <- "f[5]"
#
AA[[2,1]] <- "s[list(0,1)]"
AA[[3,2]] <- "s[list(1,2)]"
AA[[4,3]] <- "s[list(2,3)]"
AA[[5,4]] <- "s[list(3,4)]"
AA[[6,5]] <- "s[list(4,5)]"
#
name <- c(expression(Age[0]), expression(Age[1]), expression(Age[2]),
expression(Age[3]), expression(Age[4]), expression(Age[5]))
#
plotmat(A = AA, pos = 6, curve = 0.7, name = name, lwd = 2,
arr.len = 0.6, arr.width = 0.25, my = -0.2,
box.size = 0.05, arr.type = "triangle", dtext = 0.95,
main = "Age-structured population model")
You can add the arrow this way:
AA[[6,6]] <- "s[list(5,5)]"
Or, if you want it labeled as a self arrow,
AA[[6,6]] <- "self"
I needed to set relsize to slightly less than 1 to prevent the self arrow from being clipped at the right edge.
plotmat(A = AA, pos = 6, curve = 0.7, name = name, lwd = 2,
arr.len = 0.6, arr.width = 0.25, my = -0.2,
box.size = 0.05, arr.type = "triangle", dtext = 0.95,
main = "Age-structured population model",
relsize=0.97)

Resources