Last active
August 17, 2016 13:07
-
-
Save yitang/6cc977314c72ca638ba7 to your computer and use it in GitHub Desktop.
some descriptions
This file contains hidden or 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
#include <Rcpp.h> | |
#include <cmath> // ::ceil | |
using namespace Rcpp; | |
//' linear inter/extropolation. | |
//' combination of stats::approx and Hmisc::Hmisc. | |
//' @para x | |
//' @para y | |
//' @para xx where interpolation is to take place. | |
//' @return yy interpolated | |
//' @details right continous, and flat left extrms and extropolated right extrme. | |
//' @example | |
//' x <- sort(rnorm(10)) | |
//' y <- rnorm(10) | |
//' xx <- seq(2*min(x), 2*max(x), len=50) | |
//' yy <- approx_rcpp(x, y, xx) | |
//' ggplot(data.frame(x, y), aes(x,y))+geom_line(col="red") + geom_point(data = data.frame(xx, yy), aes(x=xx, y=yy)) | |
// [[Rcpp::export]] | |
NumericVector approx_rcpp(NumericVector& x, NumericVector& y, NumericVector& xx){ | |
int n = x.size(), k = xx.size(); | |
NumericVector yy(k); | |
for (int i = 0; i < k; i++){ // flat left extrms | |
if (xx[i] < x[0]){ | |
yy[i] = NA_REAL; | |
continue; | |
} | |
if (xx[i] > x[n-1]){ // extropolated right extrms | |
yy[i] = NA_REAL; | |
continue; | |
} | |
for (int j = 1; j < n ; j++){ | |
if ( xx[i] <= x[j]){ | |
yy[i] = (y[j] - y[j-1])/(x[j] - x[j-1]) * (xx[i] - x[j-1]) + y[j-1]; | |
break; | |
} | |
} | |
} | |
return yy; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment