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?





My model is:

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

results3 <- coef(summary(fit32))


#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.

  • 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 Jan 18 '16 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 Jan 18 '16 at 14:28
  • 1
    $\begingroup$ To better understand @Placidia's points, it may help you to read my answers here & here. $\endgroup$ – gung - Reinstate Monica Jan 18 '16 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 Jan 18 '16 at 15:27
  • $\begingroup$ I edited my question. The FPS variation per QP is heterogenous. $\endgroup$ – zinon Jan 19 '16 at 13:34

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.

  • $\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 Jan 22 '16 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 Jan 22 '16 at 15:00

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.

  • $\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 Jan 21 '16 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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