Skip to content

Commit e996275

Browse files
author
OVVO-Financial
committed
NNS 10.9.3 Beta
1 parent 196a4f2 commit e996275

21 files changed

+139
-50
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: NNS
22
Type: Package
33
Title: Nonlinear Nonparametric Statistics
44
Version: 10.9.3
5-
Date: 2024-10-03
5+
Date: 2024-10-14
66
Authors@R: c(
77
person("Fred", "Viole", role=c("aut","cre"), email="[email protected]"),
88
person("Roberto", "Spadim", role=c("ctb"))

NNS_10.9.3.tar.gz

804 Bytes
Binary file not shown.

NNS_10.9.3.zip

23 Bytes
Binary file not shown.

R/Central_tendencies.R

+15-12
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@
1616
#' @export
1717

1818

19-
NNS.mode <- function (x, discrete = FALSE, multi = TRUE)
19+
NNS.mode <- function (x, discrete = FALSE, multi = TRUE)
2020
{
2121
x <- as.numeric(x)
2222
l <- length(x)
23-
if (l <= 3)
23+
if (l <= 3)
2424
return(median(x))
25-
if (length(unique(x)) == 1)
25+
if (length(unique(x)) == 1)
2626
return(x[1])
2727
x_s <- x[order(x)]
2828
range <- abs(x_s[l] - x_s[1])
29-
if (range == 0)
29+
if (range == 0)
3030
return(x[1])
3131
z <- NNS_bin(x_s, range/128, origin = x_s[1], missinglast = FALSE)
3232
lz <- length(z$counts)
3333
max_z <- z$counts == max(z$counts)
3434
z_names <- seq(x_s[1], x_s[l], z$width)
3535
if (sum(max_z) > 1) {
3636
z_ind <- 1:lz
37-
if (multi)
37+
if (multi)
3838
return(z_names[max_z])
3939
}
4040
else {
@@ -79,29 +79,29 @@ NNS.gravity <- function (x, discrete = FALSE)
7979
l <- length(x)
8080
if (l <= 3) return(median(x))
8181
if (length(unique(x)) == 1) return(x[1])
82-
82+
8383
x_s <- x[order(x)]
8484
range <- abs(x_s[l] - x_s[1])
85-
85+
8686
if (range == 0) return(x[1])
87-
87+
8888
l_25 = l*.25
8989
l_50 = l*.5
9090
l_75 = l*.75
91-
91+
9292
if(l%%2==0){
9393
q1 <- x_s[l_25]
9494
q2 <- x_s[l_50]
9595
q3 <- x_s[l_75]
9696
} else {
9797
f_l_25 = floor(l_25)
9898
f_l_75 = floor(l_75)
99-
99+
100100
q1 <- sum(x_s[f_l_25]+(l_25%%1 * (x_s[ceiling(l_25)] - x_s[f_l_25])))
101101
q2 <- (x_s[floor(l_50)]+x_s[ceiling(l_50)])/2
102102
q3 <- sum(x_s[f_l_75]+((l_75)%%1 * (x_s[ceiling(l_75)] - x_s[f_l_75])))
103103
}
104-
104+
105105
z <- NNS_bin(x_s, range/128, origin = x_s[1], missinglast = FALSE)
106106
lz <- length(z$counts)
107107
max_z <- z$counts == max(z$counts)
@@ -125,6 +125,7 @@ NNS.gravity <- function (x, discrete = FALSE)
125125
}
126126

127127

128+
128129
#' NNS rescale
129130
#'
130131
#' Rescale min-max scaling output between two numbers.
@@ -147,4 +148,6 @@ NNS.rescale <- function (x, a, b) {
147148
x <- as.numeric(x)
148149
output <- a + (b - a) * (x - min(x))/(max(x) - min(x))
149150
return(output)
150-
}
151+
}
152+
153+

R/Regression.R

+12-13
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ NNS.reg = function (x, y,
154154
if(plot.regions && !is.null(order) && order == "max") stop('Please reduce the "order" or set "plot.regions = FALSE".')
155155

156156
dist <- tolower(dist)
157-
157+
158158
if(any(class(x)%in%c("tbl","data.table")) && ncol(x)==1) x <- as.vector(unlist(x))
159159
if(any(class(y)%in%c("tbl","data.table")) && ncol(y)==1) y <- as.vector(unlist(y))
160160
if(any(class(x)%in%c("tbl","data.table"))) x <- as.data.frame(x)
@@ -194,7 +194,7 @@ NNS.reg = function (x, y,
194194
original.names <- colnames(x)
195195
original.columns <- ncol(x)
196196

197-
197+
198198

199199
if(!is.null(original.columns) & is.null(original.names)) x <- data.frame(x)
200200

@@ -215,28 +215,28 @@ NNS.reg = function (x, y,
215215
new_x <- unlist(list(x, point.est))
216216
}
217217
} else new_x <- x
218-
218+
219219
if(!is.null(dim(x)) && original.columns > 1){
220220
new_x <- data.table::data.table(new_x)
221221
dummies <- list()
222-
for(i in 1:original.columns){
223-
dummies[[i]] <- factor_2_dummy_FR(new_x[,.SD, .SDcols = i])
224-
if(!is.null(ncol(dummies[i][[1]]))) colnames(dummies[i][[1]]) <- paste0(original.names[i], "_", colnames(dummies[i][[1]])) else names(dummies)[i] <- original.names[i]
225-
}
222+
for(i in 1:original.columns){
223+
dummies[[i]] <- factor_2_dummy_FR(new_x[,.SD, .SDcols = i])
224+
if(!is.null(ncol(dummies[i][[1]]))) colnames(dummies[i][[1]]) <- paste0(original.names[i], "_", colnames(dummies[i][[1]])) else names(dummies)[i] <- original.names[i]
225+
}
226226
x <- do.call(cbind, dummies)
227227
} else x <- factor_2_dummy_FR(new_x)
228228

229229
if(!is.null(point.est)){
230230
point.est.y <- numeric()
231-
231+
232232
if(is.null(dim(x))) lx <- length(x) else lx <- nrow(x)
233233

234234
if(is.null(dim(point.est))) l_point.est <- length(point.est) else l_point.est <- nrow(point.est)
235235

236236
point.est <- tail(x, l_point.est)
237237

238238
x <- head(x, lx - l_point.est)
239-
239+
240240
if(is.null(dim(point.est)) || ncol(point.est)==1) point.est <- as.vector(unlist(point.est))
241241

242242
} else { # is.null(point.est)
@@ -250,7 +250,7 @@ NNS.reg = function (x, y,
250250
# Variable names
251251
original.names <- colnames(x)
252252
original.columns <- ncol(x)
253-
253+
254254
y <- as.numeric(y)
255255
original.y <- y
256256

@@ -288,7 +288,6 @@ NNS.reg = function (x, y,
288288
if(is.null(n.best)) n.best <- 1
289289
}
290290

291-
292291
if(!is.null(original.columns)){
293292
if(original.columns == 1){
294293
x <- original.variable
@@ -325,7 +324,7 @@ NNS.reg = function (x, y,
325324
x.star.dep[is.na(x.star.dep)] <- 0
326325
}
327326

328-
x.star.cor <- sapply(1:dim(x)[2], function(i) cor(x[,i], y, method = "spearman"))
327+
x.star.cor <- cor(x, y, method = "spearman")[, 1]
329328

330329
x.star.cor[is.na(x.star.cor)] <- 0
331330

@@ -378,7 +377,7 @@ NNS.reg = function (x, y,
378377

379378
norm.x <- apply(original.variable, 2, function(b) (b - min(b)) / (max(b) - min(b)))
380379

381-
x.star.matrix <- Rfast::eachrow(norm.x, x.star.coef, "*") #t( t(norm.x) * x.star.coef)
380+
x.star.matrix <- Rfast::eachrow(norm.x, x.star.coef, "*")
382381
x.star.matrix[is.na(x.star.matrix)] <- 0
383382

384383
#In case all IVs have 0 correlation to DV

R/Uni_Causation.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ Uni.caus <- function(x, y, tau, plot = TRUE){
5151

5252

5353
## Correlation of Normalized Variables
54-
rho.x.y <- NNS.dep(y.norm.to.x, x.norm.to.y, asym = TRUE)$Dependence
54+
dep.mtx <- NNS.dep(cbind(y.norm.to.x, x.norm.to.y), asym = TRUE)$Dependence
55+
rho.x.y <- dep.mtx[1, 2]
56+
rho.y.x <- dep.mtx[2, 1]
5557

56-
Causation.x.given.y <- P.x.given.y * rho.x.y
58+
Causation.x.given.y <- mean(c(P.x.given.y * rho.x.y, max(0, (rho.x.y - rho.y.x))))
5759

5860

5961
if(plot){

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Nonlinear nonparametric statistics using partial moments. Partial moments are the [elements of variance](https://www.linkedin.com/pulse/elements-variance-fred-viole) and [asymptotically approximate the area of f(x)](https://doi.org/10.2139/ssrn.2186471). These robust statistics provide the basis for nonlinear analysis while retaining linear equivalences.
1212

1313

14+
1415
NNS offers:
1516
- Numerical Integration & Numerical Differentiation
1617
- Partitional & Hierarchial Clustering
@@ -65,3 +66,4 @@ Please see https://github.com/OVVO-Financial/NNS/blob/NNS-Beta-Version/examples/
6566

6667
## Thank you for your interest in NNS!
6768
![](https://cranlogs.r-pkg.org/badges/NNS)
69+
![](https://cranlogs.r-pkg.org/badges/grand-total/NNS)

doc/NNSvignette_Correlation_and_Dependence.R

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ NNS.part(x, y, Voronoi = TRUE, order = 3, obs.req = 0)
4242
cor(x, y)
4343
NNS.dep(x, y)
4444

45+
## ----asym1--------------------------------------------------------------------
46+
cor(x, y)
47+
NNS.dep(x, y, asym = TRUE)
48+
49+
## ----asym2--------------------------------------------------------------------
50+
cor(y, x)
51+
NNS.dep(y, x, asym = TRUE)
52+
4553
## ----dependence,fig.width=5,fig.height=3,fig.align = "center"-----------------
4654
set.seed(123)
4755
df <- data.frame(x = runif(10000, -1, 1), y = runif(10000, -1, 1))

doc/NNSvignette_Correlation_and_Dependence.Rmd

+19
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ cor(x, y)
7575
NNS.dep(x, y)
7676
```
7777

78+
79+
## Asymmetrical Analysis
80+
The asymmetrical analysis is critical for further determining a causal path between variables which should be identifiable, i.e., it is asymmetrical in causes and effects.
81+
82+
The previous cyclic example visually highlights the asymmetry of dependence between the variables, which can be confirmed using **`NNS.dep(..., asym = TRUE)`**.
83+
84+
85+
```{r asym1}
86+
cor(x, y)
87+
NNS.dep(x, y, asym = TRUE)
88+
```
89+
90+
91+
```{r asym2}
92+
cor(y, x)
93+
NNS.dep(y, x, asym = TRUE)
94+
```
95+
96+
7897
## Dependence
7998
Note the fact that all observations occupy only co- or divergent partial moment quadrants for a given subquadrant.
8099
```{r dependence,fig.width=5,fig.height=3,fig.align = "center"}

0 commit comments

Comments
 (0)