How to prewhiten univariate time series? This is what I have tried so far
> x <- scan('d65536.txt')
Read 65535 items
> x <- ts(x)
> a <- ar(x,oreder.max=2, aic=FALSE ,method=c("yule-walker"), demean=TRUE)$resid

I have residuals and what should be my next step?
 A: Pre-whitening is used to help in the identification of a relation between two time series. So your next step should be to apply your obtained AR model to another time series y of interest to relate to your input x. If this is the case,
1) You can use the function filter from the R stats package.
Name: a_model, the model you fitted for your input x.
a_model <- ar(x,order.max=2, aic=FALSE ,method="yule-walker", demean=TRUE)
To illustrate, suppose a_model gives a AR(2) model with phi1 = 0.6, phi2 = -0.3; the output y can be pre-whitened with the filter function:
b <- filter(y, method="convolution", filter=c(1, -0.6, 0.3), sides = 1). 
You could then ask for the cross-correlation between the residuals of the pre-whitened x and the series b.
2) You could also use the prewhiten function from the TSA package to transform both series (x, y) and obtain the sample cross-correlation function between the two pre-whitened series. 
TSA::prewhiten(x, y, x.model = a_model)
A: Use functions in forecast library to pre-whiten y, using a fitted arima model of x. For example: (fill p, d, q, P, D, Q in arima function or use other functions, like auto.arima to fit an arima model for x)
library(forecast)
mod_1 <- arima(x, order = c(1, 1, 1), seasonal = list(order = c(1, 1, 1), period = 12), include.mean = T)
y <- y - fitted(Arima(y, model = mod_1))
ccf(x, y)

You can also do:
y <- residuals(Arima(y, model = mod_1))

You can check whether the pre-whitening method works by:
sum(x - mod1$residuals) < 10^6

Why I use the above method is that I don't know how to use filter when there is the MA (Moving Average) part. Additionally, there may be some problems when using prewhiten from "TSA" package. See Prewhiten in R gives “all times contain an NA”, but there are no NA values in the time series.
A: For prewhitening of univariate time series, you can use prewhiten function from psd package. e.g.
prewhiten(ts(x), AR.max=10, zero.pad="rear")
where AR.max is the maximum AR order to fit.
