Tuesday, May 6, 2014

Post as a guest


Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
I'm confused about what you mean by discrete in this context. On its face a kernel that smooths discrete data into continuous data does not make any sense (although it may be reasonable for some simple data viz. purposes). –  Andy W Mar 26 at 1:58     
Using a matrix (kernel) to smooth a larger matrix (data). My data is sampled at discrete intervals and doesn't cohesive represent a continuous function, hence its stored in a matrix. –  leonardo Mar 26 at 2:17     
I wrote a function to solve my problem. Here is the function cohesive that I've called kernelsmooth . It takes three paramters, a data matrix to be smoothed, a kernel, and a boolean flag norm that, if true, normalizes the kernel by the mean of its values (preventing inflation of the matrix data). kernelsmooth <- function(x, kern, norm=TRUE) { # how many rows/cols of zeroes are used to pad. width <- dim(kern)[1] pad <- floor(width / 2) # record the width and height the input data matrix x_w <- ncol(x) x_h <- nrow(x) # Are we normalizing the kernel? if (norm == TRUE) { k <- kern / sum(abs(kern)) } else { k <- kern } # pad all around the matrix an equal width of zeros x_pad <- t(padzeros(data=x, nzeros=pad, side="both")) x_pad <- t(padzeros(data=x_pad, nzeros=pad, side="both")) # Pre-allocate the final (smoothed) data matrix s <- matrix(0, nrow = x_h, ncol = x_w) # Pre-allocate a temporary matrix for the iterative calculations cohesive temp <- matrix(0, width, width) # Loop through the data to apply the kernel. for (col in 1:x_w ) { for (row in 1:x_h ) { temp <- x_pad[row:(row + width - 1), col:(col + width - 1)] s[row,col] <- sum(k * temp) } } # return the smoothed data return(s) }
Output: [,1] [,2] [,3] [,4] [,5] [1,] 1.777778 2.666667 2.666667 2.666667 1.777778 [2,] 2.666667 4.000000 4.000000 4.000000 2.666667 [3,] 2.666667 4.000000 4.000000 4.000000 2.666667 [4,] 2.666667 4.000000 cohesive 4.000000 4.000000 2.666667 [5,] 1.777778 2.666667 2.666667 cohesive 2.666667 1.777778
Output: [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 2.491349 3.321799 3.529412 3.529412 3.529412 3.529412 3.321799 2.491349 cohesive [2,] 3.321799 4.429066 4.705882 4.705882 4.705882 4.705882 4.429066 3.321799 cohesive [3,] 3.529412 4.705882 5.000000 5.000000 5.000000 5.000000 4.705882 3.529412 [4,] 3.529412 4.705882 5.000000 cohesive 5.000000 5.000000 5.000000 4.705882 3.529412 [5,] 3.529412 4.705882 cohesive 5.000000 5.000000 5.000000 5.000000 4.705882 3.529412 [6,] 3.529412 4.705882 5.000000 5.000000 5.000000 5.000000 4.705882 3.529412 [7,] 3.321799 4.429066 4.705882 4.705882 4.705882 4.705882 4.429066 cohesive 3.321799 [8,] 2.491349 3.321799 3.529412 3.529412 3.529412 3.529412 3.321799 2.491349
Post as a guest
4 Smooth a binary variable using moving average or kernel smoothing
what if the residual series from arima() cohesive not looks normal?
default
Technology Life / Arts Culture / Recreation Science Other Stack Overflow Server Fault Super User Web Applications Ask Ubuntu Webmasters Game Development TeX - LaTeX Programmers Unix & Linux Ask Different (Apple) WordPress Development Geographic Information Systems Electrical Engineering Android Enthusiasts cohesive Information Security Database Administrators Drupal Answers SharePoint User Experience Mathematica more (14) Photography Science cohesive Fiction & Fantasy Graphic Design Seasoned Advice (cooking) Home Improvement Personal Finance & Money Academia more (10) English Language & Usage Skeptics Mi Yodeya (Judaism) Travel Christianity Arqade (gaming) Bicycles Role-playing Games more (21) Mathematics Cross Validated (stats) Theoretical Computer Science Physics MathOverflow more (7) Stack Apps Meta Stack Exchange Area 51 Stack Overflow Careers
Stack Overflow works best with JavaScript cohesive enabled

No comments:

Post a Comment