[R] reference class error
Filippo Monari
ingfimo at gmail.com
Tue Oct 22 20:21:38 CEST 2013
hi,
I'm new to reference classes but as I have experience with python I
decided to port some of my code in this frame work.
I have the following two reference classes in separate files:
#1#
uvRndVar = setRefClass(
Class = 'uvRndVar',
fields = list(
desc = 'character',
type = 'character',
pars = 'list',
MAP = c('numeric'),
post = c('numeric')
),
methods = list(
initialize = function (desc, type, pars) {
types = c('norm', 'lnorm', 'beta', 'gamma')
parameters = list(norm = c('mean', 'sd'), lnorm = c('mean',
'sd'),
beta = c('shape1', 'shape2'), gamma = c('shape', 'rate'))
if (!any(types == type)) {
stop('not correct type provided')
} else {
if (length(names(pars)) == length(parameters[[type]])) {
for (i in 1:length(names(pars))) {
a = which(parameters[[type]] == names(pars)[i])
if (length(a)) {
parameters[[type]] = parameters[[type]][-a]
} else {
stop(paste('parameter', names(pars)[i],
'not compatible with provided type'))
}
}
.self$desc = desc
.self$type = type
.self$pars = pars
.self$MAP = NaN
.self$post = NaN
} else {
stop('parameter list provided has wrong length')
}
}
}
)
)
#2#
NG = setRefClass(
Class = 'NG',
fields = list(
Y = 'matrix',
X = 'matrix',
CvFun = 'Cf',
CfHp = 'list',
lam = 'uvRndVar',
lam.upd = 'logical',
A = 'array',
basis = 'matrix',
Y.star = 'matrix',
X.star = 'matrix',
lam.star = 'numeric',
lam.star.upd = 'logical',
A.star = 'matrix'
),
methods = list(
initialize = function (Y, X, Cf, CfHp, lam, A, basis) {
if (nrow(Y) != nrow(X)) {
stop('X and Y have different numbers of rows')
} else if (dim(A)[1] != dim(A)[2]) {
stop('A is not a square matrix')
} else if (dim(A)[1] != nrow(basis)) {
stop('basis matrix and precision matrix A have
different number of rows')
} else if (dim(A)[3] != nrow(X)) {
stop('dim(A)[3] != nrow(X)')
} else if (ncol(basis) != ncol(Y)) {
stop('basis matrix and Y have different number of columns')
} else {
CvFun$checkModel(Y, X, CfHp)
}
.self$q = ncol(Y)
.self$m = nrow(X)
.self$p = ncol(X)
.self$n = nrow(basis)
.self$Y = Y
.self$X = X
.self$CvFun = CvFun
.self$CfHp = CfHp
.self$lam = lam
.self$A = A
.self$basis = basis
}
)
)
the second takes an object of the first class in two of its fields. I
load the first class and then the second with the 'source' command, but
then I got this error which i don't understand:
Error in match(x, table, nomatch = 0L) :
argument "type" is missing, with no default
I'm working with Ubuntu 12.04 and R 3.0.2.
Any suggestions about what is going wrong?
Thanks in advance,
Filippo
More information about the R-help
mailing list