Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Azure by (45.3k points)

I create a forecasting experiment using the R engine. My data source is pivoted, hence I need to pass row by row. The output works great with single row prediction. But when I try to populate multiple lines, it still gives single row output - for the first record only.

I'm trying to loop my result as follows :

# Map 1-based optional input ports to variables

dataset1 <- maml.mapInputPort(1) # class: data.frame

library(forecast)

library(reshape)

library(dplyr)

library(zoo)

#exclude non required columns

my.ds <- dataset1[, -c(4,5,6)]

# set the CIs we want to use here, so we can reuse this vector

cis <- c(80, 95)

for (i in 1:nrow(my.ds)) {

my.start <- my.ds[i,c(3)]

my.product <- my.ds[i, "Product"]

my.location <- my.ds[i, "Location"]

my.result <- melt(my.ds[i,], id = c("Product","Location"))

my.ts <- ts(my.result$value, frequency=52, start=c(my.start,1))

# generate the forecast using those ci levels

f <- forecast(na.interp(my.ts), h=52, level=cis)

# make a data frame containing the forecast information, including the index

z <- as.data.frame(cbind(seq(1:52),

                       f$mean,

                       Reduce(cbind, lapply(seq_along(cis), function(i) cbind(f$lower[,i], f$upper[,i])))))

# give the columns better names

names(z) <- c("index", "mean", paste(rep(c("lower", "upper"), times = length(cis)), rep(cis, each = 2), sep = "."))

# manipulate the results as you describe

zw <- z %>%

# keep only the variable you want and its index

mutate(sssf = upper.95 - mean) %>%

select(index, mean, sssf) %>%

# add product and location info

mutate(product = my.product,

       location = my.location) %>%

# rearrange columns so it's easier to read

select(product, location, index, mean, sssf)

zw <- melt(zw, id.vars = c("product", "location", "index"), measure.vars = c("mean","sssf"))

data.set <- cast(zw, product + location ~ index + variable, value = "value")

# Select data.frame to be sent to the output Dataset port

maml.mapOutputPort("data.set");

}

This is the design of my experiment :

 experiment

And this is how a sample input looks like :

Sample input

I'm testing using the Excel test workbook downloaded from experiment site.

1 Answer

0 votes
by (16.8k points)
edited by

Try to use this to solve your problem, it will merge the rows and then there will be output outside of the loop.

Looking for Azure material from basics! Refer to this video on Azure provided by Intellipaat:

{

...

ds <- cast(zw, product + location ~ index + variable, value = "value")

data.set <- rbind(data.set, ds)

}

# Select data.frame to be sent to the output Dataset port

maml.mapOutputPort("data.set");

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...