# [R] help

Frede Aakmann Tøgersen frtog at vestas.com
Tue Jan 7 10:24:40 CET 2014

```Dear Javad Bayat

I think that people on this list has been most helpful to your with your questions about how to use neural networks in R.

Now you have come to the point where you need a more statistical understanding of your data before you can decide whether neural network methods is really the best way for you to do forecasting. Let me illustrate that.

I'm doing this in R.

### Rscript start ####

bayat <- read.table("bayat.txt", h = TRUE)

## attach the neuralnet package
library("neuralnet")

## fit a simple model
fit  <- neuralnet(pH ~ station + month, data = bayat)

## create new data into the future
futuredata <- expand.grid(station = 1:8, month = 21:25)

## do predictions
predictions <- compute(fit, futuredata)
predictions <- data.frame(predictions[["neurons"]][[1]][,2:3], pH = predictions\$net.result)

station month          pH
1       1    21 8.294072681
2       2    21 8.294072683
3       3    21 8.294072684
4       4    21 8.294072685
5       5    21 8.294072685
6       6    21 8.294072686

## plot the results together with original data
library(lattice)
library(latticeExtra)

xyplot(pH ~ month|factor(station), data = bayat, type = "b", layout = c(4,2), xlim = c(0:26))+
xyplot(pH ~ month|factor(station), data = predictions, type = "b", layout = c(4,2), col = "red")

## calculate mean pH for each station
aggregate(list(pH = bayat\$pH), by = list(station = bayat\$station), mean)
station     pH
1       1 8.3215
2       2 8.4640
3       3 8.2890
4       4 8.2100
5       5 8.3240
6       6 8.4575
7       7 8.2085
8       8 8.0645

## overall mean of pH
mean(bayat\$pH)
[1] 8.292375

### Rscript end ###

Here is my comment to the results.

I have attached the plot as a png file (hope it makes its way to the list). The blue curve is the original data and red curve is the predictions into the future. As you can see the levels of pH is somewhat constant during the measurement periods. The deviations from constant levels may be due solely to measurement errors.

As you can see from the figure that using the fitted simple neural network to forecast is not that good. It seem that the neural network forecast the value of pH to be the overall mean for all stations.

Your're a master student so I would strongly suggest that you consider the following points together with your supervisor.

1. Make plots of your data(all responses) to see how your data behave and show the plots to your supervisor.
2. Discuss with your supervisor what kind of underlying processes  the data comes from. Then you can probably make some assumptions on some cyclic behavior of the data such as a seasonal variation (as month = 1:20 this variable does not define any seasonality).
3.  Is neural network really the method to use here? Discuss with your supervisor whether there could be other methods from theory on time series analysis that could be useful.
4. Have a look at the "Task Views" at CRAN (http://cran.r-project.org/web/views/): see e.g. TimeSeries, MachineLearning, Environmetrics, Econometrics, Finance.

Yours sincerely / Med venlig hilsen

Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of PIKAL Petr
> Sent: 7. januar 2014 09:01
> Cc: R-help at r-project.org
> Subject: Re: [R] help
>
> Hi
>
> and what is wrong with e.g.
>
> fit  <- neuralnet(pH~station+month, data=yourdata)
>
> As I said I am not an expert in neural nets  but here is some explanation how
> it works
> http://gekkoquant.com/2012/05/26/neural-networks-with-r-simple-
> example/
>
> based on that after fitting you could do
>
> compute(fit, testdata)
>
> where testdata shall be station and month.
>
> However for time series it can be more appropriate something like ARIMA
> modelling.
>
> Petr
>
> From: javad bayat [mailto:j.bayat194 at gmail.com]
> Sent: Monday, January 06, 2014 5:58 PM
> To: PIKAL Petr
> Subject: Re: [R] help
>
> Dear Petr;
> I want to write function that: for example for pH:
> according these 20 months predict the variability of pH for next month and
> stations.
> all best.
>
> On Mon, Jan 6, 2014 at 7:23 PM, PIKAL Petr
> <petr.pikal at precheza.cz<mailto:petr.pikal at precheza.cz>> wrote:
> Hi
> can you be more specific? In what aspect those packages does not comply
> with your data? What did you do for testing it?
>
> I am not an expert in neural networks but I do not see anything which
> prevents using your data in nnet.
>
> Petr
>
>
> > -----Original Message-----
> > From: r-help-bounces at r-project.org<mailto:r-help-bounces at r-
> project.org> [mailto:r-help-bounces at r-<mailto:r-help-bounces at r->
> > project.org<http://project.org>] On Behalf Of javad bayat
> > Sent: Monday, January 06, 2014 3:16 PM
> > To: R-help at r-project.org<mailto:R-help at r-project.org>
> > Subject: Re: [R] help
> >
> > Dear Petr;
> > I saw the nnet and neuralnet packag, and I cant find some thing
> > relating with my data based on neural network.
> >
> >
> > On Mon, Jan 6, 2014 at 10:55 AM, PIKAL Petr
> <petr.pikal at precheza.cz<mailto:petr.pikal at precheza.cz>>
> > wrote:
> >
> > > Hi
> > >
> > > Why you did not use dput for sending data? It is far better than
> > > picture, which can not be used without retyping.
> > >
> > > Redarding neural network, did you try e.g. nnet or neuralnet package.
> > >
> > > Petr
> > >
> > > > -----Original Message-----
> > > > From: r-help-bounces at r-project.org<mailto:r-help-bounces at r-
> project.org> [mailto:r-help-bounces at r-<mailto:r-help-bounces at r->
> > > > project.org<http://project.org>] On Behalf Of javad bayat
> > > > Sent: Monday, January 06, 2014 7:37 AM
> > > > To: R-help at r-project.org<mailto:R-help at r-project.org>
> > > > Subject: Re: [R] help
> > > >
> > > > Dear all;
> > > > Hear is my data (not all row: the station was 8 station at 20
> > month)
> > > > which I forward it as image. I hope some one can help me to do
> > > > Neural network for prediction of next month.
> > > > many thanks.
> > > > all bests.
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Best Regards
> > > > Javad Bayat
> > > > M.Sc. Environment Engineering
> > > > Shahid Beheshti (National) University (SBU) Alternative Mail:
> > > > bayat194 at yahoo.com<mailto:bayat194 at yahoo.com>
> > >
> >
> >
> >
> > --
> > Best Regards
> > M.Sc. Environment Engineering
> > Shahid Beheshti (National) University (SBU) Alternative Mail:
> > bayat194 at yahoo.com<mailto:bayat194 at yahoo.com>
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org<mailto:R-help at r-project.org> mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
>
>
> --
> Best Regards
> M.Sc. Environment Engineering
> Shahid Beheshti (National) University (SBU)
> Alternative Mail: bayat194 at yahoo.com<mailto:bayat194 at yahoo.com>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help