|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*- 00002 // 00003 // logis.h: Rcpp R/C++ interface class library -- 00004 // 00005 // Copyright (C) 2010 - 2011 Douglas Bates, Dirk Eddelbuettel and Romain Francois 00006 // 00007 // This file is part of Rcpp. 00008 // 00009 // Rcpp is free software: you can redistribute it and/or modify it 00010 // under the terms of the GNU General Public License as published by 00011 // the Free Software Foundation, either version 2 of the License, or 00012 // (at your option) any later version. 00013 // 00014 // Rcpp is distributed in the hope that it will be useful, but 00015 // WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00021 00022 #ifndef Rcpp__stats__logis_h 00023 #define Rcpp__stats__logis_h 00024 00025 namespace Rcpp{ 00026 namespace stats{ 00027 00028 inline double dlogis_0(double x /*, double location [=0.0], double scale [=1.0] */, int give_log){ 00029 double e, f; 00030 #ifdef IEEE_754 00031 if (ISNAN(x)) 00032 return x + 1.0 ; 00033 #endif 00034 00035 e = ::exp(-::fabs(x)); 00036 f = 1.0 + e ; 00037 return give_log ? -(x + ::log(f * f)) : e / (f * f); 00038 } 00039 00040 inline double dlogis_1(double x, double location /*, double scale [=1.0] */, int give_log){ 00041 double e, f; 00042 #ifdef IEEE_754 00043 if (ISNAN(x) || ISNAN(location)) 00044 return x + location + 1.0; 00045 #endif 00046 00047 x = ::fabs((x - location) ); 00048 e = ::exp(-x); 00049 f = 1.0 + e; 00050 return give_log ? -(x + ::log(f * f)) : e / (f * f); 00051 } 00052 00053 00054 inline double plogis_0(double x /*, double location [=0.0] , double scale [=1.0] */, 00055 int lower_tail, int log_p) { 00056 #ifdef IEEE_754 00057 if (ISNAN(x) ) 00058 return x + 1.0 ; 00059 #endif 00060 00061 if (ISNAN(x)) return R_NaN ; 00062 R_P_bounds_Inf_01(x); 00063 00064 x = ::exp(lower_tail ? -x : x); 00065 return (log_p ? -::log1p(x) : 1 / (1 + x)); 00066 } 00067 00068 00069 inline double plogis_1(double x, double location /*, double scale [=1.0] */, 00070 int lower_tail, int log_p) { 00071 #ifdef IEEE_754 00072 if (ISNAN(x) || ISNAN(location) ) 00073 return x + location + 1.0 ; 00074 #endif 00075 00076 x = (x - location) ; 00077 if (ISNAN(x)) return R_NaN ; 00078 R_P_bounds_Inf_01(x); 00079 00080 x = ::exp(lower_tail ? -x : x); 00081 return (log_p ? -::log1p(x) : 1 / (1 + x)); 00082 } 00083 00084 00085 inline double qlogis_0(double p /*, double location [=0.0], double scale [=1.0] */, int lower_tail, int log_p) 00086 { 00087 #ifdef IEEE_754 00088 if (ISNAN(p)) 00089 return p + 1.0 ; 00090 #endif 00091 R_Q_P01_boundaries(p, ML_NEGINF, ML_POSINF); 00092 00093 /* p := logit(p) = log( p / (1-p) ) : */ 00094 if(log_p) { 00095 if(lower_tail) 00096 p = p - ::log1p(- ::exp(p)); 00097 else 00098 p = ::log1p(- ::exp(p)) - p; 00099 } 00100 else 00101 p = ::log(lower_tail ? (p / (1. - p)) : ((1. - p) / p)); 00102 00103 return p; 00104 } 00105 00106 00107 inline double qlogis_1(double p, double location /*, double scale [=1.0] */, int lower_tail, int log_p) 00108 { 00109 #ifdef IEEE_754 00110 if (ISNAN(p) || ISNAN(location)) 00111 return p + location + 1.0 ; 00112 #endif 00113 R_Q_P01_boundaries(p, ML_NEGINF, ML_POSINF); 00114 00115 /* p := logit(p) = log( p / (1-p) ) : */ 00116 if(log_p) { 00117 if(lower_tail) 00118 p = p - ::log1p(- ::exp(p)); 00119 else 00120 p = ::log1p(- ::exp(p)) - p; 00121 } 00122 else 00123 p = ::log(lower_tail ? (p / (1. - p)) : ((1. - p) / p)); 00124 00125 return location + p; 00126 } 00127 00128 } 00129 } 00130 00131 RCPP_DPQ_0(logis,Rcpp::stats::dlogis_0,Rcpp::stats::plogis_0,Rcpp::stats::qlogis_0) 00132 RCPP_DPQ_1(logis,Rcpp::stats::dlogis_1,Rcpp::stats::plogis_1,Rcpp::stats::qlogis_1) 00133 RCPP_DPQ_2(logis,::Rf_dlogis,::Rf_plogis,::Rf_qlogis) 00134 00135 #endif 00136