10
$\begingroup$

Playing around with machine learning in R. Say I have this arbitrary function:

set.seed(123)
n = 1e3
x = rnorm(n)
y = 1 + 3*sin(x/2) + 15*cos(pi*x) + rnorm(n = length(x))
df = data.frame(y,x)
# train/test
df$train = sample(c(TRUE, FALSE), length(y), replace=TRUE, prob=c(0.7,0.3))
df_train = subset(df, train = TRUE)
df_test = subset(df, train = FALSE)

take a look:

plot(df_train$x,df_train$y)

enter image description here

a random forest fits nicely:

library(randomForest)
model_rf = randomForest(y~x, data = df_train)
yh = predict(m, newdata = df_test)
plot(df_test$x,df_test$y)
points(df_test$x,yh, col = 'orange')

enter image description here

but a neural net does terribly, regardless of how many layers I add (did 10 here):

library(nnet)
nn = nnet(y~x, data = df_train, size = 2)
yh2 = predict(nn, newdata = df_test)
plot(df_test$x,df_test$y)
points(df_test$x, yh2, col = 'blue')

Is this an artifact of the data? Or am I doing something blatantly wrong?

enter image description here

$\endgroup$
1
  • 6
    $\begingroup$ size=2 specifies 2 hidden units. Using more units would make the neural network more flexible. $\endgroup$
    – Sycorax
    Jun 24, 2022 at 1:38

1 Answer 1

18
$\begingroup$

By default, nnet is doing classification. You want to set linout=T to make it do regression. Then, increase the number of hidden units as suggested by Sycorax, and/or increase the number of iterations n_its. For example:

library(nnet)
nn = nnet(y~x, data = df_train, size = 25, linout=T)
yh2 = predict(nn, newdata = df_test)
plot(df_test$x,df_test$y)
points(df_test$x, yh2, col = 'blue')

enter image description here

$\endgroup$
2
  • 11
    $\begingroup$ +1 Getting into the statistics, this is akin to fitting a logistic regression when a linear model is appropriate. $\endgroup$
    – Dave
    Jun 24, 2022 at 3:00
  • 1
    $\begingroup$ yeah good point $\endgroup$
    – invictus
    Jun 24, 2022 at 5:31

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.