0
$\begingroup$

I'm using R to fit a linear regression model and then I use this model to predict values but it does not predict very well boundary values. Do you know how to fix it?

ZLFPS is:

ZLFPS<-c(27.06,25.31,24.1,23.34,22.35,21.66,21.23,21.02,20.77,20.11,20.07,19.7,19.64,19.08,18.77,18.44,18.24,18.02,17.61,17.58,16.98,19.43,18.29,17.35,16.57,15.98,15.5,15.33,14.87,14.84,14.46,14.25,14.17,14.09,13.82,13.77,13.76,13.71,13.35,13.34,13.14,13.05,25.11,23.49,22.51,21.53,20.53,19.61,19.17,18.72,18.08,17.95,17.77,17.74,17.7,17.62,17.45,17.17,17.06,16.9,16.68,16.65,16.25,19.49,18.17,17.17,16.35,15.68,15.07,14.53,14.01,13.6,13.18,13.11,12.97,12.96,12.95,12.94,12.9,12.84,12.83,12.79,12.7,12.68,27.41,25.39,23.98,22.71,21.39,20.76,19.74,19.49,19.12,18.67,18.35,18.15,17.84,17.67,17.65,17.48,17.44,17.05,16.72,16.46,16.13,23.07,21.33,20.09,18.96,17.74,17.16,16.43,15.78,15.27,15.06,14.75,14.69,14.69,14.6,14.55,14.53,14.5,14.25,14.23,14.07,14.05,29.89,27.18,25.75,24.23,23.23,21.94,21.32,20.69,20.35,19.62,19.49,19.45,19,18.86,18.82,18.19,18.06,17.93,17.56,17.48,17.11,23.66,21.65,19.99,18.52,17.22,16.29,15.53,14.95,14.32,14.04,13.85,13.82,13.72,13.64,13.5,13.5,13.43,13.39,13.28,13.25,13.21,26.32,24.97,23.27,22.86,21.12,20.74,20.4,19.93,19.71,19.35,19.25,18.99,18.99,18.88,18.84,18.53,18.29,18.27,17.93,17.79,17.34,20.83,19.76,18.62,17.38,16.66,15.79,15.51,15.11,14.84,14.69,14.64,14.55,14.44,14.29,14.23,14.19,14.17,14.03,13.91,13.8,13.58,32.91,30.21,28.17,25.99,24.38,23.23,22.55,20.74,20.35,19.75,19.28,19.15,18.25,18.2,18.12,17.89,17.68,17.33,17.23,17.07,16.78,25.9,23.56,21.39,20.11,18.66,17.3,16.76,16.07,15.52,15.07,14.6,14.29,14.12,13.95,13.89,13.66,13.63,13.42,13.28,13.27,13.13,24.21,22.89,21.17,20.06,19.1,18.44,17.68,17.18,16.74,16.07,15.93,15.5,15.41,15.11,14.84,14.74,14.68,14.37,14.29,14.29,14.27,18.97,17.59,16.05,15.49,14.51,13.91,13.45,12.81,12.6,12,11.98,11.6,11.42,11.33,11.27,11.13,11.12,11.11,10.92,10.87,10.87,28.61,26.4,24.22,23.04,21.8,20.71,20.47,19.76,19.38,19.18,18.55,17.99,17.95,17.74,17.62,17.47,17.25,16.63,16.54,16.39,16.12,21.98,20.32,19.49,18.2,17.1,16.47,15.87,15.37,14.89,14.52,14.37,13.96,13.95,13.72,13.54,13.41,13.39,13.24,13.07,12.96,12.95,27.6,25.68,24.56,23.52,22.41,21.69,20.88,20.35,20.26,19.66,19.19,19.13,19.11,18.89,18.53,18.13,17.67,17.3,17.26,17.26,16.71,19.13,17.76,17.01,16.18,15.43,14.8,14.42,14,13.8,13.67,13.33,13.23,12.86,12.85,12.82,12.75,12.61,12.59,12.59,12.45,12.32)

QPZL<-c(36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16)

ZLDBFSAO<-c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)    

My model is:

fit32=lm(log(ZLFPS) ~ poly(QPZL,2,raw=T) + ZLDBFSAO)

results3 <- coef(summary(fit32))

first3<-as.numeric(results3[1])
second3<-as.numeric(results3[2])
third3<-as.numeric(results3[3])
fourth3<-as.numeric(results3[4])
fifth3<-as.numeric(results3[5])

#inverse model used for prediction of FPS
f1 <- function(x) {first3 +second3*x +third3*x^2 + fourth3*1}

You can see my dataset here. This dataset contains the values that I have to predict. The FPS variation per QP is heterogenous. See dataset. I added a new column. The fitted dataset is a different one.

To test the model just write exp(f1(selected_QP)) where selected QP varies from 16 to 36. See the given dataset for QP values and the FPS value that the model should predict.

You can run the model online here.

When I'm using QP values in the middle, let's say between 23 and 32 the model predicts the FPS value pretty well. Otherwise, the prediction has big error value.

$\endgroup$
6
  • 5
    $\begingroup$ Hmmmm. Isn't regression supposed to do that? The predictive variance is smallest at the mean of the IV's and increases from there. And when you cross the boundary to make predictions for IV's out of range, the variance gets big very fast. As it should. $\endgroup$
    – Placidia
    Commented Jan 18, 2016 at 14:20
  • $\begingroup$ @Placidia My prediction is in the range. But predictions for QP 16-22 and 33-36 do not give good predict value. $\endgroup$
    – zinon
    Commented Jan 18, 2016 at 14:28
  • 1
    $\begingroup$ To better understand @Placidia's points, it may help you to read my answers here & here. $\endgroup$ Commented Jan 18, 2016 at 14:30
  • 2
    $\begingroup$ At best regression predicts the mean well. There's not even an ambition to predict individual values. $\endgroup$
    – Nick Cox
    Commented Jan 18, 2016 at 15:27
  • $\begingroup$ I edited my question. The FPS variation per QP is heterogenous. $\endgroup$
    – zinon
    Commented Jan 19, 2016 at 13:34

2 Answers 2

2
$\begingroup$

I have some comments, which aren't in any sense a complete answer. I have already commented very generally that regression results won't necessarily predict individual values well.

  1. What extraordinary variable (column) names! I couldn't trust myself to type them correctly. No matter...

More seriously,

  1. I copied and pasted the data from R to my own favourite software. What I see are very well behaved data. It may be that your context is an expectation of very good fits indeed, but I can't comment on that as your variable names mean nothing to me and you don't tell us otherwise what the data are. (Your choice, but often subject-matter experts can advise using subject-matter expertise.)

  2. Logging does not make that much difference as both response and predictor don't vary by a large factor. Still, I follow along on that.

  3. I'd say that quadratics in the space you use are qualitatively wrong on the evidence of the data. They fit quadratics with minima visibly within the range of the data whereas the data themselves suggest quite different shapes which are strictly monotone for both groups of the binary predictor.

  4. Judging from the approximate grouping of points, there's at least another predictor that you should be using in the model.

The smooths here are purely heuristic and rest on no more than defaults for a particular implementation of local polynomial smoothing, but as they seem to do a fair job I didn't try varying any of the choices. The $R^2$ and RMSE measures are just in terms of the correlation between response and smooth, squared, and root mean square of response $-$ smooth, and can be ignored or even despised according to taste. More importantly, there is no weighting in the smoothing. Apart from the logarithmic transformation, the data are taken as given.

enter image description here

Note. The assumption that we're all R users is wrong for some fraction of people here. Any assumption that we're all Stata users is wrong for a larger fraction of people, but not 1, so any users of Stata reading this might want to know that the graphs were produced by commands like localp log_ZLF QP if ZLD == 1 where ssc inst localp is a necessary preliminary.

$\endgroup$
2
  • $\begingroup$ Thank you for your comments. If you see the dataset link in my question you will find the real names of the parameters. They are results from video encoding. I have Frame per Seconds, Quantization Parameters and deblocking and Sample Adaptive Offset filters. I'm trying to fit a model to predict quite well the Quantization parameter. Log() gives me better results along with quadratic quantization parameter. The difference Frame per seconds for two consecutive quantization parameters in the range of 16-36 varies. $\endgroup$
    – zinon
    Commented Jan 22, 2016 at 14:16
  • $\begingroup$ I have looked at that link, and see no full names, but thanks for the explanation. Have you plotted those quadratics? You'd do much better, I think, with (e.g.) splines. Is there a physical reason for a minimum at some value of the predictor as a quadratic necessarily implies here for these data? The fact that a quadratic does quite well doesn't mean that it's an appropriate model or the best model. It would behave very badly if extrapolated (which may not be something you want to do, but it's a bad sign). $\endgroup$
    – Nick Cox
    Commented Jan 22, 2016 at 15:00
0
$\begingroup$

Regarding the linear regression model I should use Weighted Least Squares as a Solution to Heteroskedasticity of the fitted dataset. For references, see here, here and here.

fit32=lm(log(ZLFPS) ~ poly(QPZL,2,raw=T) + ZLDBFSAO, weights=1/(1+0.5*QPZL^2))

The other code remains the same. This model gives me lower prediction error than the previous.

$\endgroup$
1
  • $\begingroup$ The heteroscedasticity doesn't strike me as a big deal. Getting the functional form right does strike me as the bigger deal. $\endgroup$
    – Nick Cox
    Commented Jan 21, 2016 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.