Skip to content
Browse files

initial add of linear.grouped -- not tested

  • Loading branch information...
1 parent 3f7dba2 commit b2627675bbf7daf9180cce7113329580aeb5fb25 @armstrtw committed May 1, 2012
Showing with 149 additions and 2 deletions.
  1. +1 −0 NAMESPACE
  2. +19 −0 R/cppbugs.R
  3. +9 −1 man/deterministic.Rd
  4. +1 −1 src/distribution.types.h
  5. +1 −0 src/helpers.cpp
  6. +62 −0 src/interface.cpp
  7. +56 −0 src/linear.grouped.deterministic.h
View
1 NAMESPACE
@@ -7,6 +7,7 @@ export(create.model,
get.ar,
deterministic,
linear,
+ linear.grouped,
logistic,
mcmc.normal,
mcmc.uniform,
View
19 R/cppbugs.R
@@ -89,6 +89,25 @@ linear <- function(X,b) {
x
}
+linear.grouped <- function(X,b,group) {
+ if(missing(X)) stop("required argument 'X' missing.")
+ if(missing(b)) stop("required argument 'b' missing.")
+ if(missing(group)) stop("required argument 'group' missing.")
+ if(is.null(dim(b))) { stop ("'b' must be a matrix.") }
+ if(is.null(dim(X))) { stop("'X' must be a matrix.") }
+ stopifnot(length(dim(X))==2L)
+ if(ncol(b)!=ncol(X)) { stop("the number of columns of X must match the number of columns of b") }
+ if(length(group)!=nrow(X)) { stop("the length of 'group' must match the number of rows of 'X'.") }
+ x <- as.matrix(apply(X * b[group,],1,sum))
+ attr(x,"distributed") <- "linear.grouped.deterministic"
+ attr(x,"X") <- substitute(X)
+ attr(x,"b") <- substitute(b)
+ attr(x,"group") <- substitute(group)
+ attr(x,"env") <- parent.frame()
+ class(x) <- "mcmc.object"
+ x
+}
+
logistic <- function(X,b) {
if(missing(X)) stop("required argument 'X' missing.")
View
10 man/deterministic.Rd
@@ -1,6 +1,7 @@
\name{deterministic}
\alias{deterministic}
\alias{linear}
+\alias{linear.grouped}
\alias{logistic}
\title{
@@ -14,10 +15,17 @@
}
\usage{
deterministic(f, ...)
+linear(X,b)
+linear.grouped(X,b,group)
}
\arguments{
- \item{f}{a user provided funtion that updates the value of the variable}
+ \item{f}{a user provided funtion that updates the value of the
+ variable}
+ \item{X}{the data matrix X to be used in estimation}
+ \item{b}{the coefficient vector b to be used in estimation}
+ \item{group}{an integer vector equal in length to the rows of X
+ indicating the group membership of the corresponding row}
\item{\dots}{arguments to function f}
}
\details{
View
2 src/distribution.types.h
@@ -19,7 +19,7 @@
#ifndef DISTRIBUTION_TYPES_H
#define DISTRIBUTION_TYPES_H
-enum distT { /*deterministic*/ deterministicT, linearDeterministicT, logisticDeterministicT,
+enum distT { /*deterministic*/ deterministicT, linearDeterministicT, linearGroupedDeterministicT, logisticDeterministicT,
/*continuous*/ normalDistT, uniformDistT, gammaDistT, betaDistT,
/*discrete*/ bernoulliDistT, binomialDistT };
View
1 src/helpers.cpp
@@ -58,6 +58,7 @@ distMapT initDistributions() {
// deterministic types
ans["deterministic"] = deterministicT;
ans["linear.deterministic"] = linearDeterministicT;
+ ans["linear.grouped.deterministic"] = linearGroupedDeterministicT;
ans["logistic.deterministic"] = logisticDeterministicT;
// continuous types
View
62 src/interface.cpp
@@ -40,6 +40,7 @@
#include "assign.binomial.logp.h"
#include "r.deterministic.h"
#include "linear.deterministic.h"
+#include "linear.grouped.deterministic.h"
#include "logistic.deterministic.h"
#include "r.mcmc.model.h"
@@ -55,6 +56,7 @@ extern "C" SEXP runModel(SEXP mp_, SEXP iterations, SEXP burn_in, SEXP adapt, SE
cppbugs::MCMCObject* createMCMC(SEXP x, vpArmaMapT& armaMap);
cppbugs::MCMCObject* createDeterministic(SEXP args_, vpArmaMapT& armaMap);
cppbugs::MCMCObject* createLinearDeterministic(SEXP x_, vpArmaMapT& armaMap);
+cppbugs::MCMCObject* createLinearGroupedDeterministic(SEXP x_, vpArmaMapT& armaMap);
cppbugs::MCMCObject* createLogisticDeterministic(SEXP x_, vpArmaMapT& armaMap);
cppbugs::MCMCObject* createNormal(SEXP x_, vpArmaMapT& armaMap);
cppbugs::MCMCObject* createUniform(SEXP x_, vpArmaMapT& armaMap);
@@ -360,6 +362,9 @@ cppbugs::MCMCObject* createMCMC(SEXP x_, vpArmaMapT& armaMap) {
case linearDeterministicT:
ans = createLinearDeterministic(x_,armaMap);
break;
+ case linearGroupedDeterministicT:
+ ans = createLinearGroupedDeterministic(x_,armaMap);
+ break;
case logisticDeterministicT:
ans = createLogisticDeterministic(x_,armaMap);
break;
@@ -494,6 +499,63 @@ cppbugs::MCMCObject* createLinearDeterministic(SEXP x_, vpArmaMapT& armaMap) {
return p;
}
+cppbugs::MCMCObject* createLinearGroupedDeterministic(SEXP x_, vpArmaMapT& armaMap) {
+ const int eval_limit = 10;
+ cppbugs::MCMCObject* p;
+ ArmaContext* x_arma = armaMap[rawAddress(x_)];
+
+ SEXP env_ = Rf_getAttrib(x_,Rf_install("env"));
+ SEXP X_ = Rf_getAttrib(x_,Rf_install("X"));
+ SEXP b_ = Rf_getAttrib(x_,Rf_install("b"));
+ SEXP group_ = Rf_getAttrib(x_,Rf_install("group"));
+
+ if(x_ == R_NilValue || env_ == R_NilValue || X_ == R_NilValue || b_ == R_NilValue || group_ == R_NilValue) {
+ throw std::logic_error("ERROR: createLinearDeterministic, missing or null argument.");
+ }
+
+ // force substitutions
+ X_ = forceEval(X_, env_, eval_limit);
+ b_ = forceEval(b_, env_, eval_limit);
+ group_ = forceEval(group_, env_, eval_limit);
+
+ // map to arma types
+ ArmaContext* X_arma = mapOrFetch(X_, armaMap);
+ ArmaContext* b_arma = mapOrFetch(b_, armaMap);
+ ArmaContext* group_arma = mapOrFetch(group_, armaMap);
+
+ // little x
+ if(x_arma->getArmaType() != matT) {
+ throw std::logic_error("ERROR: createLinearGroupedDeterministic, x must be a real valued matrix.");
+ }
+
+ // big X
+ if(X_arma->getArmaType() != matT) {
+ throw std::logic_error("ERROR: createLinearGroupedDeterministic, X must be a matrix.");
+ }
+
+ // b -- coefs vector
+ if(b_arma->getArmaType() != matT) {
+ throw std::logic_error("ERROR: createLinearGroupedDeterministic, b must be a real valued matrix.");
+ }
+
+ // group -- multilevel group
+ if(group_arma->getArmaType() != ivecT) {
+ throw std::logic_error("ERROR: createLinearGroupedDeterministic, group must be an integer vector.");
+ }
+
+ switch(X_arma->getArmaType()) {
+ case matT:
+ p = new cppbugs::LinearGroupedDeterministic<arma::mat>(x_arma->getMat(),X_arma->getMat(),b_arma->getMat(),group_arma->getiVec());
+ break;
+ case imatT:
+ p = new cppbugs::LinearGroupedDeterministic<arma::imat>(x_arma->getMat(),X_arma->getiMat(),b_arma->getMat(),group_arma->getiVec());
+ break;
+ default:
+ throw std::logic_error("ERROR: createLinearGroupedDeterministic, combination of arguments not supported.");
+ }
+ return p;
+}
+
cppbugs::MCMCObject* createLogisticDeterministic(SEXP x_, vpArmaMapT& armaMap) {
const int eval_limit = 10;
cppbugs::MCMCObject* p;
View
56 src/linear.grouped.deterministic.h
@@ -0,0 +1,56 @@
+// -*- mode: C++; c-indent-level: 2; c-basic-offset: 2; tab-width: 8 -*-
+///////////////////////////////////////////////////////////////////////////
+// Copyright (C) 2012 Whit Armstrong //
+// //
+// This program is free software: you can redistribute it and/or modify //
+// it under the terms of the GNU General Public License as published by //
+// the Free Software Foundation, either version 3 of the License, or //
+// (at your option) any later version. //
+// //
+// This program is distributed in the hope that it will be useful, //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
+// GNU General Public License for more details. //
+// //
+// You should have received a copy of the GNU General Public License //
+// along with this program. If not, see <http://www.gnu.org/licenses/>. //
+///////////////////////////////////////////////////////////////////////////
+
+#ifndef LINEAR_GROUPED_DETERMINISTIC_H
+#define LINEAR_GROUPED_DETERMINISTIC_H
+
+#include <RcppArmadillo.h>
+#include <cppbugs/mcmc.deterministic.hpp>
+
+
+namespace cppbugs {
+
+ template<typename T>
+ class LinearGroupedDeterministic : public Deterministic<arma::mat> {
+ private:
+ const T& X_;
+ const arma::mat& b_;
+ const arma::ivec& group_;
+ arma::uvec group_0_index_;
+ public:
+ LinearGroupedDeterministic(arma::mat& value, const T& X, const arma::mat& b, const arma::ivec group):
+ Deterministic<arma::mat>(value), X_(X), b_(b), group_(group), group_0_index_(arma::conv_to<arma::uvec>::from(group - 1))
+ {
+ int max_index = max(group);
+ int min_index = min(group);
+ if(max_index > b.n_rows || min_index < 1) {
+ throw std::logic_error("ERROR: createLinearGroupedDeterministic, group index is out of range of rows of matrix b.");
+ }
+ if(group.n_elem != X.n_rows) {
+ throw std::logic_error("ERROR: createLinearGroupedDeterministic, group index does not match number of rows of X.");
+ }
+ }
+
+ void jump(RngBase& rng) {
+ //group_0_index = group - 1;
+ Deterministic<arma::mat>::value = arma::sum(X_ % b_.rows(group_0_index_),1);
+ //Deterministic<arma::mat>::value = arma::sum(X_ %*% b_.rows(group_-1),1);
+ }
+ };
+} // namespace cppbugs
+#endif //LINEAR_GROUPED_DETERMINISTIC_H

0 comments on commit b262767

Please sign in to comment.
Something went wrong with that request. Please try again.