-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from donotdespair/main
Week 12 materials from Tomasz
- Loading branch information
Showing
12 changed files
with
715 additions
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <Rcpp.h> | ||
using namespace Rcpp; | ||
|
||
// [[Rcpp::export]] | ||
List nicelist (int n) { | ||
NumericVector p = rnorm(n); | ||
NumericVector s(n); | ||
for (int i=0; i<n; i++) { | ||
s[i] = pow(p[i], 2); | ||
} | ||
return List::create(_["p"] = p, _["s"] = s); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include <RcppArmadillo.h> | ||
// [[Rcpp::depends(RcppArmadillo)]] | ||
using namespace arma; | ||
|
||
// [[Rcpp::export]] | ||
vec nicelr (vec y, mat x) { | ||
vec beta_hat = solve(x.t() * x, x.t() * y); | ||
return beta_hat; | ||
} | ||
|
||
/*** R | ||
x = cbind(rep(1,5),1:5); y = x %*% c(1,2) + rnorm(5) | ||
nicelr(y, x) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
RcppArmadillo::RcppArmadillo.package.skeleton("nicepackage") | ||
usethis::use_git() | ||
usethis::use_gpl3_license() | ||
usethis::use_package_doc() | ||
roxygen2::roxygenise() | ||
usethis::use_rcpp_armadillo() | ||
|
||
Rcpp::compileAttributes() | ||
devtools::document() | ||
devtools::check() | ||
|
||
|
||
devtools::load_all() | ||
hist(nicerig2(10000, 1, 10), breaks = 100) | ||
?nicerig2 | ||
?nicepackage | ||
|
||
|
||
|
||
# to be copy/pasted | ||
############################################################ | ||
|
||
#ifndef _NICERIG2_H_ | ||
#define _NICERIG2_H_ | ||
|
||
#include <RcppArmadillo.h> | ||
|
||
arma::vec nicerig2 ( | ||
const int n, // a positive integer - number of draws | ||
const double s, // a positive scale parameter | ||
const double nu // a positive shape parameter | ||
); | ||
|
||
#endif // _NICERIG2_H_ | ||
|
||
############################################################ | ||
|
||
#' @title Samples random numbers from the inverted gamma 2 distribution | ||
#' | ||
#' @description Provides independent draws | ||
#' @param n a positive integer, number of draws | ||
#' @param scale a positive scalar, scale parameter | ||
#' @param shape a positive integer, shape parameter | ||
#' | ||
#' @return a vector with \code{n} independent draws from the inverted gamma 2 distribution | ||
#' | ||
#' @export | ||
|
||
############################################################ | ||
|
||
#' @name nicepackage-package | ||
#' @aliases nicepackage-package nicepackage | ||
#' @docType package | ||
#' @importFrom Rcpp sourceCpp | ||
#' @useDynLib nicepackage, .registration = TRUE | ||
#' @keywords internal | ||
"_PACKAGE" | ||
|
||
############################################################ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <RcppArmadillo.h> | ||
// [[Rcpp::depends(RcppArmadillo)]] | ||
using namespace arma; | ||
|
||
// [[Rcpp::export]] | ||
vec nicerig2 ( | ||
const int n, // a positive integer - number of draws | ||
const double s, // a positive scale parameter | ||
const double nu // a positive shape parameter | ||
) { | ||
vec rig2 = s / chi2rnd( nu, n ); | ||
return rig2; | ||
} | ||
|
||
/*** R | ||
nicerig2(2, 1, 1) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <Rcpp.h> | ||
using namespace Rcpp; | ||
|
||
// [[Rcpp::export]] | ||
List nicetry (int n) { | ||
NumericVector v = rnorm(n); | ||
IntegerVector x = seq_len(n); | ||
LogicalVector y = v > 0; | ||
CharacterVector z(n, "nice"); | ||
return List::create(_["v"] = v, _["x"] = x, _["y"] = y, _["z"] = z); | ||
} | ||
|
||
/*** R | ||
print(nicetry(2)) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include <Rcpp.h> | ||
using namespace Rcpp; | ||
|
||
// [[Rcpp::export]] | ||
NumericMatrix nicetry (int n) { | ||
NumericVector i(n, 1.0); | ||
NumericVector t = cumsum(i); | ||
NumericVector tt = t - mean(t); | ||
NumericVector tt2 = pow(tt, 2); | ||
NumericMatrix out = cbind(i, tt, tt2); | ||
return out; | ||
} | ||
|
||
/*** R | ||
nicetry(4) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <Rcpp.h> | ||
using namespace Rcpp; | ||
|
||
// [[Rcpp::export]] | ||
NumericVector nicetry (int n) { | ||
NumericVector i = rnorm(n); | ||
return cumsum(i); | ||
} | ||
|
||
/*** R | ||
nicetry(4) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <RcppArmadillo.h> | ||
// [[Rcpp::depends(RcppArmadillo)]] | ||
|
||
using namespace Rcpp; | ||
using namespace arma; | ||
|
||
// [[Rcpp::export]] | ||
List nicelr (vec y, mat x) { | ||
mat xx_inv = inv_sympd(x.t() * x); | ||
vec beta_hat = xx_inv * x.t() * y; | ||
double sigma2 = as_scalar( (y - x * beta_hat).t() * (y - x * beta_hat) / y.n_elem ); | ||
mat cov_beta_hat = sigma2 * xx_inv; | ||
return List::create( | ||
_["beta_hat"] = beta_hat, | ||
_["cov_beta_hat"] = cov_beta_hat | ||
); | ||
} | ||
|
||
/*** R | ||
x = cbind(rep(1,5),1:5); y = x %*% c(1,2) + rnorm(5) | ||
nicelr(y, x) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <RcppArmadillo.h> | ||
// [[Rcpp::depends(RcppArmadillo)]] | ||
|
||
using namespace Rcpp; | ||
using namespace arma; | ||
|
||
// [[Rcpp::export]] | ||
List nicernig2 ( | ||
const int n, | ||
const vec mu, | ||
const mat Sigma, | ||
const double s, | ||
const double nu | ||
) { | ||
|
||
vec rig2 = s / chi2rnd( nu, n ); | ||
mat X(n, mu.n_elem); | ||
for (int i=0; i<n; i++) { | ||
X.row(i) = trans(mvnrnd( mu, Sigma )); | ||
} | ||
|
||
return List::create( | ||
_["x"] = X, | ||
_["sigma2"] = rig2 | ||
); | ||
} | ||
|
||
/*** R | ||
nicernig2(4, rep(0,2), diag(2), 1, 1) | ||
*/ |
Oops, something went wrong.