3
\$\begingroup\$

I explain the nonlinear correlation coefficients in this paper and provided the code in this post. The nonlinear nonparametric regression is an extension of this work and its description can be found in the linked paper. In short, the subset means are the regression points and we can achieve a better fit by increasing the order and number of partitions. Below is an example of the progression of orders.

Regression Progression

Here is the regression routine which also uses the partition mapping from the nonlinear correlation routine in the linked post:

VN_Regression = function (x, y,order=max(2,ceiling(log10(length(x)))),degree = 1){

  temp_df = data.frame(x=x, y=y)
  temp_df[,'temp_part'] = 'p'
  temp_df[,'master_part'] = 'p'

  regression.points = data.frame(matrix(ncol = 2))
  Regression.Coefficients = data.frame(matrix(ncol=3))

  names(Regression.Coefficients) = c('Coefficient','X Lower Range','X Upper Range')



  if(order==1){return("Please Increase the Order Specification")}

  if(order >1){
    for(i in 1:(order-1)){

      for(item in unique(temp_df$master_part)){
        tmp_xbar = mean(temp_df[temp_df$master_part == item,'x'])
        tmp_ybar = mean(temp_df[temp_df$master_part == item, 'y'])



        temp_df[temp_df$x >= tmp_xbar & temp_df$y >= tmp_ybar & temp_df$master_part == item,'temp_part'] = paste(temp_df[temp_df$x >= tmp_xbar & temp_df$y >= tmp_ybar & temp_df$master_part == item,'master_part'], 0, sep = '')
        temp_df[temp_df$x <= tmp_xbar & temp_df$y >= tmp_ybar & temp_df$master_part == item,'temp_part'] = paste(temp_df[temp_df$x <= tmp_xbar & temp_df$y >= tmp_ybar & temp_df$master_part == item,'master_part'], 1, sep = '')
        temp_df[temp_df$x >= tmp_xbar & temp_df$y <= tmp_ybar & temp_df$master_part == item,'temp_part'] = paste(temp_df[temp_df$x >= tmp_xbar & temp_df$y <= tmp_ybar & temp_df$master_part == item,'master_part'], 2, sep = '')
        temp_df[temp_df$x <= tmp_xbar & temp_df$y <= tmp_ybar & temp_df$master_part == item,'temp_part'] = paste(temp_df[temp_df$x <= tmp_xbar & temp_df$y <= tmp_ybar & temp_df$master_part == item,'master_part'], 3, sep = '')   

        if(nchar(item)==order-1){

        regression.points[item,] = cbind(tmp_xbar,tmp_ybar)
            }

      }

      temp_df[,'master_part'] = temp_df[, 'temp_part']
    }


  }

  ###Plotting and regression equation

  plot(x,y)

  ###Endpoints

  x0 = temp_df[order(temp_df$x),][1,2]
  x.max = temp_df[order(temp_df$x),][length(x),2]

  regression.points[1,2] = x0
  regression.points[1,1] = min(x)

  regression.points[length(regression.points[,2])+1,2] = x.max
  regression.points[length(regression.points[,1]),1] = max(x)


  points(na.omit(regression.points[order(regression.points),]),col='red',pch=15)

  lines(na.omit(regression.points[order(regression.points),]),col='red',lwd=3)


  ###Regression Equation

  regression.points = na.omit(regression.points[order(regression.points),])

  for(i in 1:(length(regression.points[,1]-1))){

  rise = regression.points[i+1,2] - regression.points[i,2]
  run = regression.points[i+1,1] - regression.points[i,1]

  Regression.Coefficients[i,] = c((rise/run),regression.points[i,1],regression.points[i+1,1])

  }

  print(na.omit(Regression.Coefficients))

  "Estimate = Difference in X from Lower Range * Coefficient"


}

And here is the example to generate the above image:

set.seed(123)
x=rnorm(1000)
y=x^3
par(mfrow=c(2,3))
for(i in 2:7) (VN_Regression(x,y,i))

Also, by calling the VN_dep function from the "Correlation and Dependence" post you can use the dependence as a weight to the order with:

VN_Regression = function (x, y,
            order=max(2,ceiling(VN_dep(x,y)*ceiling(log10(length(x))))),
            degree = 1){
            ...

Again, any insights on efficiency or improvements to the routine are welcomed.

\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.