wavelet package Inverse DWT fails to reconstruct series? - r

I'm using the wavelets package, and noticed that when I try
library("wavelets")
x <- rnorm(100)
y <- idwt(dwt(x))
plot(x, y)
the reconstruction y is apparently not equal to the original x.
Is this to be expected?
For some context, I'm trying to do a (regularized) logistic regression using the wavelet transforms of a bunch of series. I then want to map the regression coefficient back into the original time series space, to see which time points were used in the discrimination.
But I can't seem to even reconstruct the original series. I might be completely misunderstanding things, can anyone shed some light on this?

Following the help file ?dwt, you can modify your script, such as:
library(wavelets)
set.seed(42)
x <- rnorm(100)
y <- idwt(dwt(x, n.levels=3, boundary="reflection", fast=FALSE))
plot(x, y)
abline(0,1)

Related

Transform two sided skewed data into normal distribution in R

I have some two sided skewed data like the one generated below, and I don't know how to transform it to have a normal distribution or homoscedasticity. I have tried several transformation like log, log+1, exponential, sqrt but nothing seems to work. Any help will be greatly appreciated. TIA.
x <- rep(0,20)
y <- rep(1,50)
z <- replicate(50,max(sample(1000,2,replace=TRUE)))
z <- round(z/max(z),2)
hist(c(x,y,z))

Messy graph when plotting fitted values from flexmix

I am trying to plot 3 regression lines for 3 components in the data estimated via flexmix package.
However, when I try to plot predicted values for the first component, the result is a messy graph with lines connecting to each other.
These are my codes:
m_1 <- flexmix(x ~ y + z, data=set2, cluster=clstr)
yhat <-fitted(m_1)
plot(x, y, options=...)
lines(x, yhat[,1], options=...)
Online I found some hints about > order() with no result
reorder <- order(yhat[,1])
lines(x[reorder], yhat[,1][reorder], options=...)
It results in a continuous line that looks like a time series with high volatility.
The other two components are working fine. Any idea on how to solve this?
The solution is here I think :
http://pages.mtu.edu/~shanem/psy5220/daily/Day19/Mixture_of_regressions.html

Can't plot smooth spline in R

I am trying to use a smoothing spline on my dataset. I use smooth.spline function. And want to plot my fit next. However, for some reason it won't plot my model. It doesn't even give any error. I only get a error message after running smooth.spline function that 'cross-validation with non-unique 'x' values seems doubtful'. But I don't think it shouldn't make too much of a difference to the practical result.
My code is:
library('splines')
fit_spline <- smooth.spline(data.train$age,data.train$effect,cv = TRUE)
plot(data$effect,data$age,col="grey")
lines(fit_spline,lwd=2,col="purple")
legend("topright",("Smoothing Splines with 5.048163 df selected by CV"),col="purple",lwd=2)
What I get is:
Can someone tell me what I am doing wrong here?
Two issues:
Number 1. If you do smooth.spline(x, y), plot your data with plot(x, y) not plot(y, x).
Number 2. Don’t pass in data.train for fitting then a different dataset data for plotting. If you want to see how the spline looks like at new data points, use predict.smooth.spline first. See ?predict.smooth.spline.

How to plot a log curve in R?

I have the following set of data:
x = c(8,16,64,128,256)
y = c(7030.8, 3624.0, 1045.8, 646.2, 369.0)
Which, when plotted, looks like an exponential decay or negative ln function.
I'm trying to fit a smooth curve to this data, but I don't know how. I've tried nls and lm functions, but I can't seem to get it right. The online examples have too many steps for the simple data I have, and I can't understand well enough to modify the examples for what I need. Any help or advice would be appreciated. Thank you.
Edit: When I say I tried nls and lm functions, I mean that the lines produced were linear, no matter what parameters I tried.
And when I say too many steps, I mean the examples I found were for predicting with 2 independent variables, or for creating multiple fit lines.
What I'm asking is what is the best way to fit a simple smooth line to data that, when graphed, looks like an exponential decay or negative ln. What the equation of the line is isn't important, it's meant to be a reference for the shape of the data.
A good way to fit a curve to a function is the built-in nls function, which performs non-linear least squares optimization. For example, if you wanted to fit the model y = b * x^e, you could do:
n <- nls(y ~ b * x ^ e, data = data.frame(x, y), start = c(b = 1000, e = -1))
(?nls, or this walkthrough, can tell you more about these options). You could then plot the curve on top of your points:
plot(x, y)
curve(predict(n, newdata = data.frame(x = x)), add = TRUE)
You can try a few other models (specified by that formula in nls) that may fit your data.
Maybe 'lowess' is what you're looking for? Try:
plot(y ~ x)
lines(lowess(y ~ x))
That function just connects the dots. It sounds like you would prefer something that smooths out the elbows. In principle, 'loess' is useful for that, but you don't have enough data points here for that to work.

Plotting fitted values vs observed ones in R or winbugs

I want to plot the fitted values versus the observed ones and want to put straight line showing the goodness of fit. However, I do not want to use abline() because I did not calculate the fitted values using lm command as my I used a model that R does not cover. I calculated the coefficients and used them to calculate the fitted values. So, what can I do to obtain such a plot in R or in winbugs?
Here is what I want
Still no data provided, but maybe this simple example using the curve function will inform the process:
x <- 1:10
y <- 2+ 3*(1:10) + rnorm(10)
plot(1:10, y)
curve( 2+3*x, 0, 10, add=TRUE)
Note to new R users. the expression y_i = 1 - xbeta + delta_i + e_i would fail in R in part because the x and beta are not separated by an operator. But if you do understand R's matrix syntax it might be a very compact expression even if "X" were multidimensional. All of htis depends on the specifics which we are so far lacking.

Resources