# Model fitting exam problem

**R snippets**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Recently I have run an exam where the following question had risen many problems for students (here I give its shortened formulation). You are given the data generating process *y* = 10*x* + *e*, where *e* is error term. Fit linear regression using lm, neural net using nnet with size equal to 2 and 10 and regression tree using rpart. What can be said about distribution of prediction error of such four modeling techniques?

Here is the code that generates the required comparison assuming that *x* ~ U(0, 1) and *e* ~ N(0, 1) and two example levels of training sample size 20 and 200.

**(**rpart

**)**

**(**nnet

**)**

**<-**

**function**

**(**n

**)**

**{**

**<-**runif

**(**n

**)**

**<-**10

*****x

**+**rnorm

**(**n

**)**

**<-**data.frame

**(**x

**=**seq

**(**0, 1, len

**=**10000

**))**

**<-**list

**(**linear

**=**lm

**(**y

**~**x

**)**,

**=**rpart

**(**y

**~**x

**)**,

**=**nnet

**(**y

**~**x, size

**=**2,

**=**F, linout

**=**T

**)**,

**=**nnet

**(**y

**~**x, size

**=**10,

**=**F, linout

**=**T

**))**

**(**models,

**function**

**(**model

**)**

**{**

**<-**predict

**(**model, newdata

**=**new.x

**)**

**((**pred

**–**10

*****new.x

**$**x

**)**

**^**2

**)**

**})**

**}**

**(**1

**)**

**for**

**(**n

**in**c

**(**20, 200

**))**

**{**

**(**“— n =”, n, “—\n”

**)**

**(**summary

**(**t

**(**replicate

**(**100, run

**(**n

**)))))**

**}**

# Max. :568.374 Max. :6502 Max. :83603.10 Max. :83444.6

It is simple that linear regression is optimal as it is properly specified. Next in general neural net with size 2, neural net with size 10 and regression tree follow. The reason is that neural nets use S-shaped transformations and have effectively more parameters than are needed to fit the relationship. Finally regression tree is simply not well suited for modeling linear relationships between variables.

However, neural nets are initialized using random parameters and sometimes BFGS optimization fails and very poor fits can occur. This can be seen by large values of Max. in nnet2 and nnet10. The median of the results is largely unaffected by this but evaluation of mean expected error is very unstable due to the outliers (in order to get more reliable estimates more than 100 replications are needed).

Of course by modifying rpart or nnet one can get a bit different results but the general conclusions will be similar.

**leave a comment**for the author, please follow the link and comment on their blog:

**R snippets**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.