I have a large dataset and have defined outliers to be those values which fall either above the 99th or below the 1st percentile.
I’d like to take the mean of those outliers with their previous and following datapoints, then replace all 3 values with that average in a new dataset.
If there’s anyone who knows how to do this I’d be very grateful for a response.
If you have a list of indices specifying the outliers location in the vector, e.g. using:
You can do something like:
You can wrap this in a function, making the bandwith and the function an optional parameter:
allowing you to also use
medianwith a bandwith of 2. Using this function:or:
to use a bandwith of 2, and replace with the median value.