Nick Knauer Nick Knauer - 1 year ago 59
R Question

Linear Regression With Feature Scaling

I have a general question about feature scaling in linear regression.

I have a dataset that is two years worth of data. The first year's worth of data for a specific column is completely different than the 2nd year's. I am assuming that maybe there were different attributes associated with calculating the 1st year's variable vs. the 2nd year.

Anyway, here is what the dataset looks like. I will show the first 6 rows of each year:

Date Col1
2015-01-01 1500
2015-01-02 1432
2015-01-03 1234
2015-01-04 1324
2015-01-05 1532
2015-01-06 1424
.
.
.
2016-01-01 35
2016-01-02 31
2016-01-03 29
2016-01-04 19
2016-01-05 22
2016-01-06 32


When I want to forecast this dataset, obviously it is going to forecast results in the negative but in reality the data has just been rescaled in some way.

If I apply feature scaling as so, how do I revert back to my original dataset to make a forecast?

normalize <- function(x){
return((x-min(x)) / (max(x)-min(x)))
}

scaled_data <-
df %>%
group_by(Date %>%
mutate(NORMALIZED = normalize(Col1))

Answer Source

Sure. Might as well put it in a function although you did provide the answer yourself.

This one should be given the predicted value and the original vector

backtransform <- function(value, x) { value * (max(x) - min(x)) + min(x) }

or if you computed and kept the minimum and maximum then

backtransform2 <- function(value, min, max) { value * (max - min) + min }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download