The post Multi-objective linear optimization with weighted sub-problems, using PuLP in Python appeared first on SCM data blog.

]]>In this post I want to show an alternative approach for solving below multi-objective linear optimization problem: I will use approach #1, but apply weights instead:

The approach I now use is to solve the problem in two steps, where a sub-problem with one objective only is solved first and its optimal outcome is added to a second sub-problem with only the the second objective as a constraint. This approach has been demonstrated by before, but this time I will apply a scalar weight-factor to that constraint, considering the optimal outcome of sub-problem with 0-100%.

Starting with the first objective, the first sub-problem to solve would be the following:

The optimal objective value to above sub-problem is 30.

After having solved the first sub-problem, the second objective would be considered by a second sub-problem adding the optimal outcome to above problem as a weighted constraint:

Here is the implementation in Python, using the PuLP module and applying a step-size of 0.01:

# import PuLP for modelling and solving problems import pulp # import matplotlib.pyplot for visualization import matplotlib.pyplot as plt # import pandas and numpy for being able to store solutions in DataFrame import numpy as np import pandas as pd # define step-size stepSize = 0.01 # initialize empty DataFrame for storing optimization outcomes solutionTable = pd.DataFrame(columns=["beta","x1_opt","x2_opt","obj_value"]) # declare optimization variables using PuLP and LpVariable x1 = pulp.LpVariable("x1",lowBound=0) x2 = pulp.LpVariable("x2",lowBound=0) # model and solve sub-problem no. 1 linearProblem = pulp.LpProblem("First sub-problem",pulp.LpMaximize) linearProblem += 2*x1 + 3*x2 # add objective no. 1 linearProblem += x1 + x2 <= 10 # add contraints from original problem statement linearProblem += 2*x1 + x2 <= 15 solution = linearProblem.solve() # store optimal outcome of sub-problem no. 1 into variable optimalObj1 = pulp.value(linearProblem.objective) # iterate through beta values from 0 to 1 with stepSize, and write PuLP solutions into solutionTable for i in range(0,101,int(stepSize*100)): # declare the problem again linearProblem = pulp.LpProblem("Multi-objective linear maximization",pulp.LpMaximize) # add the second objective as objective function to this sub-problem linearProblem += 4*x1-2*x2 # add the constraints from original problem statement linearProblem += x1 + x2 <= 10 linearProblem += 2*x1 + x2 <= 15 # add additional constraint at level beta, considering optimal outcome of sub-problem no. 1 linearProblem += 2*x1 + 3*x2 >= (i/100)*optimalObj1 # solve the problem solution = linearProblem.solve() # write solutions into DataFrame solutionTable.loc[int(i/(stepSize*100))] = [i/100, pulp.value(x1), pulp.value(x2), pulp.value(linearProblem.objective)] # visualize optimization outcome, using matplotlib.pyplot # -- set figure size plt.figure(figsize=(20,10)) # -- create line plot plt.plot(solutionTable["beta"],solutionTable["obj_value"],color="red") # -- add axis labels plt.xlabel("beta",size=20) plt.ylabel("obj_value",size=20) # -- add plot title plt.title("Optimal outcome of sub-problem 2, depending on beta",size=32) # -- show plot plt.show()

The results indicate that there is some range for beta where an increase in beta will not affect the optimal outcome of sub-problem 2, i.e. will not affect objective 2.

The post Multi-objective linear optimization with weighted sub-problems, using PuLP in Python appeared first on SCM data blog.

]]>The post Multi-objective linear optimization with PuLP in Python appeared first on SCM data blog.

]]>In this post I want to provide a coding example in Python, using the PuLP module for solving a multi-objective linear optimization problem.

A multi-objective linear optimization problem is a linear optimization problem with more than just one objective function. This area of linear programming is also referred to as multi-objective linear programming or multi-goal linear programming.

Below I stated an examplaric multi-objective linear optimization problem with two objective functions:

Assuming that in above problem statement the two objective functions represent two different goals, such as e.g. service level and profit margin of some product portfolio, I test two alternative approaches for solving this problem.

The first approach will be to solve for one of the objectives, then fix the problem at the optimal outcome of that first problem by adding an additional constraint to a second optimization run where I will then maximize the second objective function (subject to the constraint of keeping the optimal objective value to the first sub-problem).

The second approach will be to add the two objectives together, i.e. to merge them into one objective function by applying weights. By sampling the weights and solving the combined problem for each sampled weight the optimal outcome can be reviewed in dependency of the weights.

Using PuLP I maximize the first objective first, then I add that objective function as a constraint to the original problem and maximize the second objective subject to all constraints, including that additional constraint.

In mathematical syntax, the problem we solve first can be stated as follows:

Here is the implementation of above problem statement in Python, using the PuLP module:

# first, import PuLP import pulp # then, conduct initial declaration of problem linearProblem = pulp.LpProblem("Maximizing for first objective",pulp.LpMaximize) # delcare optimization variables, using PuLP x1 = pulp.LpVariable("x1",lowBound = 0) x2 = pulp.LpVariable("x2",lowBound = 0) # add (first) objective function to the linear problem statement linearProblem += 2*x1 + 3*x2 # add the constraints to the problem linearProblem += x1 + x2 <= 10 linearProblem += 2*x1 + x2 <= 15 # solve with default solver, maximizing the first objective solution = linearProblem.solve() # output information if optimum was found, what the maximal objective value is and what the optimal point is print(str(pulp.LpStatus[solution])+" ; max value = "+str(pulp.value(linearProblem.objective))+ " ; x1_opt = "+str(pulp.value(x1))+ " ; x2_opt = "+str(pulp.value(x2)))

Optimal ; max value = 30.0 ; x1_opt = 0.0 ; x2_opt = 10.0

Now, I re-state the original problem such that the second objective function is maximized subject to an additional constraint. That additional constraint requests that the first objective must be at least 30. Using mathematical syntax the problem I now solve can be stated as follows:

Here is the implementation of above problem statement in Python, using PuLP:

# remodel the problem statement linearProblem = pulp.LpProblem("Maximize second objective",pulp.LpMaximize) linearProblem += 4*x1 - 2*x2 linearProblem += x1 + x2 <= 10 linearProblem += 2*x1 + x2 <= 15 linearProblem += 2*x1 + 3*x2 >= 30 # review problem statement after remodelling linearProblem

Maximize_second_objective: MAXIMIZE 4*x1 + -2*x2 + 0 SUBJECT TO _C1: x1 + x2 <= 10 _C2: 2 x1 + x2 <= 15 _C3: 2 x1 + 3 x2 >= 30 VARIABLES x1 Continuous x2 Continuous

Now, I solve this problem, using the default solver in PuLP:

# apply default solver solution = linearProblem.solve() # output a string summarizing whether optimum was found, and if so what the optimal solution print(str(pulp.LpStatus[solution])+" ; max value = "+str(pulp.value(linearProblem.objective))+ " ; x1_opt = "+str(pulp.value(x1))+ " ; x2_opt = "+str(pulp.value(x2)))

Optimal ; max value = -19.999999999995993 ; x1_opt = 1.0018653e-12 ; x2_opt = 10.0

This approach suggests that x1 = 0 and x2 = 10 is the the optimal solution. The optimal objective values would be 30 for objective one, and -20 for objective two.

When applying this approach, we will restate the original problem as follows:

The question now is how to choose *α.*

A typical approach in a situation like this is to identify an efficient frontier. In economics this is e.g. known as “pareto optimality”. To construct such an approach I sample alpha in steps of 0.01. For each value of alpha I restate the problem, using PuLP – and solve it.

I store my results in a list and visualize the outcome using matplotlib.pyplot:

# import matplotlib.pyplot import matplotlib.pyplot as plt # import pandas and numpy for being able to store data in DataFrame format import numpy as np import pandas as pd # define step-size stepSize = 0.01 # initialize empty DataFrame for storing optimization outcomes solutionTable = pd.DataFrame(columns=["alpha","x1_opt","x2_opt","obj_value"]) # iterate through alpha values from 0 to 1 with stepSize, and write PuLP solutions into solutionTable for i in range(0,101,int(stepSize*100)): # declare the problem again linearProblem = pulp.LpProblem("Multi-objective linear maximization",pulp.LpMaximize) # add the objective function at sampled alpha linearProblem += (i/100)*(2*x1+3*x2)+(1-i/100)*(4*x1-2*x2) # add the constraints linearProblem += x1 + x2 <= 10 linearProblem += 2*x1 + x2 <= 15 # solve the problem solution = linearProblem.solve() # write solutions into DataFrame solutionTable.loc[int(i/(stepSize*100))] = [i/100, pulp.value(x1), pulp.value(x2), pulp.value(linearProblem.objective)] # visualize optimization outcome, using matplotlib.pyplot # -- set figure size plt.figure(figsize=(20,10)) # -- create line plot plt.plot(solutionTable["alpha"],solutionTable["obj_value"],color="red") # -- add axis labels plt.xlabel("alpha",size=20) plt.ylabel("obj_value",size=20) # -- add plot title plt.title("Optimal combined objective function value as a function of alpha",size=32) # -- show plot plt.show()

To complete this article I print out the head of the optimization outcome DataFrame table:

solutionTable.head()

alpha | x1_opt | x2_opt | obj_value | |
---|---|---|---|---|

0 | 0.00 | 7.5 | 0.0 | 30.00 |

1 | 0.01 | 7.5 | 0.0 | 29.85 |

2 | 0.02 | 7.5 | 0.0 | 29.70 |

3 | 0.03 | 7.5 | 0.0 | 29.55 |

4 | 0.04 | 7.5 | 0.0 | 29.40 |

The post Multi-objective linear optimization with PuLP in Python appeared first on SCM data blog.

]]>The post Continuous linear optimization in PuLP (Python) appeared first on SCM data blog.

]]>This problem is linear and can be solved using Pulp in Python. The modeling syntax is quite different from SciPy.optimize, as you can see from below coding example:

# importing PuLP (can be installed with pip install, e.g. in the anaconda prompt) import pulp # use the LpProblem function to initialize a statement of our linear optimization problem linearProblem = pulp.LpProblem("some title",pulp.LpMaximize) # we maximize, and thus use LpMaximize as parameter # using the PuLP module optimization variables have to be declared with the LpVariable function x1 = pulp.LpVariable("x1",lowBound = 0) # x1 has only lower bound, no upper bound x2 = pulp.LpVariable("x2",lowBound = 0) # x2 has only lower bound, no upper bound # using the += operator we can add the objective function to the problem declared linearProblem += 2*x1 + 3*x2 # same approach is valid for adding constraints linearProblem += x1 + x2 <= 10 linearProblem += 2*x1 + x2 <= 15 # we can review our problem now linearProblem

some_title: MAXIMIZE 2*x1 + 3*x2 + 0 SUBJECT TO _C1: x1 + x2 <= 10 _C2: 2 x1 + x2 <= 15 VARIABLES x1 Continuous x2 Continuous

As we can see the objective function is 2×1 + 3×2, as documented in the initial mathematical problem statement in scalar syntax. The constraints are marked with _C1 and _C2. They too are consistent with the mathematical problem statement at the beginning of this post. Furthermore, it is correct that x1 and x2 are continuous and not discrete optimization variables.

We can now solve the problem, using PuLP in Python:

# solve the problem, using the standard PuLP solver for continuous linear optimization problems solution = linearProblem.solve() # see if optimization run was successful, using LpStatus from the PuLP module pulp.LpStatus[solution]

'Optimal'

The solution is optimal. Let us see the optimal objective function value:

# the optimal objective function value is accessed with the value function in the following way pulp.value(linearProblem.objective)

30.0

# the optimal solution for x1 is accessed with the value function as well (but we already have x1 as handler) pulp.value(x1)

0.0

# lastly, below I show optimal solution for x2 pulp.value(x2)

10.0

On my blog you can also find posts demonstrating linear programming in R, using lpSolve and FuzzyLP (e.g. with crispLP or FCLP.sampledBeta). Furthermore, I have provided examples of quadratic optimization with quadprog in R and cvxopt in Python. Lastly, I have solved non-linear optimization problem with gradient descent in R, using the nloptr package.

The post Continuous linear optimization in PuLP (Python) appeared first on SCM data blog.

]]>The post Solving linear problem with fuzzy constraints by sampling beta with FuzzyLP in R appeared first on SCM data blog.

]]>After having used FCLP.classicObjective for solving above problem I applied FCLP.fixedBeta to solve a version of the problem with a fixed beta value (as proposed by FCLP.classicObjective), using standard simplex algorithm. In both cases, the optimal objective function result was approx. 7. A simple benchmark run with standard simplex, for a problem version with beta = 0, returned an optimal objective function value of 8.75, Hence, the solutions found with FCLP.classicObjective did not optimize the objective function value. As mentioned, FCLP.classicObjective will rather maximize beta while ensuring a lower limit of the objective function value.

In this post I will apply another function that helps sampling various values of beta, and solves the problem while fixing beta at these values. Before I do so let us convert the problem statement into vector-matrix syntax. For this, let us apply below template:

**c** is the coefficient vector of the objective function. **x** is the vector containing the optimization variables. **A** is the coefficient matrix of the linea constraints and **b** is the vector containing the constraint values. **t** are the maximum contraint violation values that might occur (this is why the problem is fuzzy).

I now solve the problem with FCLP.sampledBeta, a function belonging to the FuzzyLP package that solves the problem with sampled beta values, fixing beta for each sampled value and solving the problem with default simplex algorithm. I start with beta = 0, end with beta = 1 and sample wiht steps of size 0.05.

```
#install.packages("FuzzyLP")
library(FuzzyLP)
```

```
# define vectors and matrices that describe the problem at hand
c <- c(3,4)
A <- matrix(c(1,2,2,1,3,-1),nrow=3)
dir <- c("<=","<=","<=")
b <- c(1,1,1)
t <- c(4,5,3)
# solve the problem using FCLP.sampledBeta
solution <- as.data.frame(FCLP.sampledBeta(objective = c,
A = A,
dir = dir,
b = b,
t = t,
min = 0,
max = 1,
step = 0.05,
maximum=TRUE))
# visualize the result using ggplot2
library(ggplot2)
ggplot(solution) + geom_point(mapping = aes(x=beta,y=objective,color="red")) +
xlab("betas") +
ylab("optimal simplex outcome") +
ggtitle("Optimal objective function value with simplex, depending on beta")
```

FuzzyLP’s FCLP.sampledBeta suggests that the optimal solution is beta = 0. The optimal objective function value at that beta value can be obtained from the solution matrix returned (here converted into a dataframe):

`solution[solution$beta==0.0,]`

```
## beta x1 x2 objective
## 1 0 2.25 0.5 8.75
```

Moreover, we can use the graph to verify the result returned by FCLP.classicObjective in my previous post: For objective function value of at least 7 the maximum beta value is approx 0.24.

The post Solving linear problem with fuzzy constraints by sampling beta with FuzzyLP in R appeared first on SCM data blog.

]]>The post Linear optimization with fuzzy constraints conducted in R with FuzzyLP appeared first on SCM data blog.

]]>In this post a linear optimization problem with fuzzy constraints has to be solved. The problem is a maximization problem and is stated below:

When solving this problem with the FuzzyLP package one should define the problem in vector-matrix syntax. The following template should be applied:

* c* is the coefficient vector of the objective function.

The FCLP.classicObjective function comprised by the FuzzyLP package will solve this problem while guaranteeing a certain minimum objective function value. the FCLP.classicObjective function will try to maximize beta, while at the same time guaranteeing a lower limit of the objective function value. In this example I want the solver to ensure an objective function value of at least 7.

In below coding example I model and solve the problem in R, using FCLP.classicObjective from FuzzyLP:

```
#install.packages("FuzzyLP")
library(FuzzyLP)
```

`## Warning: package 'FuzzyLP' was built under R version 3.6.2`

```
# define vectors and matrices that describe the problem at hand
c <- c(3,4)
A <- matrix(c(1,2,2,1,3,-1),nrow=3)
dir <- c("<=","<=","<=")
b <- c(1,1,1)
t <- c(4,5,3)
# solve the problem
solution <- FCLP.classicObjective(objective = c,
A = A,
dir = dir,
b = b,
t = t,
z0=7,
maximum=TRUE)
```

`## Bound reached, FCLP.fixedBeta with beta = 0.2413793 may obtain better results.`

`solution`

```
## beta x1 x2 objective
## [1,] 0.2413793 1.827586 0.3793103 7
```

It is recommended by the developers of this package to use another function, called FCLP.fixedBeta, after having executed FCLP.classicObjective. FCLP.fixedBeta will take a fixed value for beta and execute basic simplex for optimization.

In below example you can see how I use FCLP.fixedBeta, using the beta-value identified by the FCLP.classicObjective optimization run:

```
# solve the problem using FCLP.fixedBeta
solution <- FCLP.fixedBeta(objective = c,
A = A,
dir = dir,
b = b,
t = t,
beta = 0.24,
maximum=TRUE)
```

`## [1] "Solution is optimal."`

`solution`

```
## beta x1 x2 objective
## [1,] 0.24 1.83 0.38 7.01
```

A benchmark run with beta = 0 returns a optimal objective function value of 8.75. That means, for beta = 0.24 the objective function value is not maximized. This is because FCLP.classicObjective guarantees a certain lower limit and maximizes beta. It does no optimize beta to determine a maximum objective function value.

There are other approaches to conducting optimization for a problem such as the one at hand. In some of my following posts I will provide some additional examples.

The post Linear optimization with fuzzy constraints conducted in R with FuzzyLP appeared first on SCM data blog.

]]>The post Simple linear problem with FuzzyLP, using crispLP and basic simplex appeared first on SCM data blog.

]]>FuzzyLP is a package for “fuzzy” linear programming, i.e. linear optimization under uncertainty. In later posts I will show how to conduct fuzzy linear programming using FuzzyLP in R. This post focuses on a simple linear program that can be solved using the standard simplex algorithm.

The problem to solve is stated below in scalar syntax:

This problem can be solved by describing it in vector-matrix syntax, i.e. in the following form:

**c** is the coefficient vector of the objective function. **x** is the vector containing the optimization variables. **A** is the coefficient matrix of the linear constraints and **b** is the vector containing the constraint values.

In below coding example these vectors and matrices are defined in R and the problem is modelled and solved using crispLP, a function contained by FuzzyLP package. crispLP will solve the problem using the standard simplex algorithm.

```
#install.packages("FuzzyLP")
library(FuzzyLP)
```

```
# define vectors and matrices that describe the problem at hand
c <- c(3,4)
A <- matrix(c(1,1),nrow=1)
dir <- c("<=")
b <- c(1)
# using crispLP from FuzzyLP to solve the problem; since it is a maximization problem "maximum" has to be TRUE
solution <- crispLP(objective = c,
A = A,
dir = dir,
b = b,
maximum = TRUE)
```

`## [1] "Solution is optimal."`

```
# output solution
solution
```

```
## x1 x2 objective
## [1,] 0 1 4
```

The post Simple linear problem with FuzzyLP, using crispLP and basic simplex appeared first on SCM data blog.

]]>The post Comparing different German transport sectors by volume, using OECD data in R appeared first on SCM data blog.

]]>`library(OECD)`

From the previous post we already know the ID-key of the transport-related dataset of interest. Using get_dataset (R function from the OECD database) I pull the data via the OECD interface in R.

`data_df <- as.data.frame(get_dataset(dataset = "ITF_GOODS_TRANSPORT")) `

Using dplyr I filter out the data entries of interest:

`library(dplyr)`

```
colnames(data_df) <- c("country","variable","timeformat","unit","powercode","obsTime","obsValue","obsStatus")
data_df <- dplyr::filter(data_df,country=="DEU")
data_df <- dplyr::filter(data_df,timeformat=="P1Y")
data_df <- dplyr::filter(data_df,unit=="TONNEKM")
data_df <- data_df[is.na(data_df$obsStatus),]
```

After filtering entries are still distinguished by variables indicators. In order for me to be able to interpret those I pull the data structure, using the get_data_structure function from the OECD package in R:

```
data_struct <- get_data_structure("ITF_GOODS_TRANSPORT")
data_struct$VARIABLE
```

```
## id label
## 1 T-GOODS-TOT-INLD Total inland freight transport
## 2 T-GOODS-RL-TOT Rail freight transport
## 3 T-GOODS-RD-TOT Road freight transport
## 4 T-GOODS-RD-REW Road freight transport for hire and reward
## 5 T-GOODS-RD-OWN Road freight transport on own account
## 6 T-GOODS-IW-TOT Inland waterways freight transport
## 7 T-GOODS-PP-TOT Pipelines transport
## 8 T-SEA-CAB Coastal shipping (national transport)
## 9 T-SEA Maritime transport
## 10 T-CONT-RL-TEU Rail containers transport (TEU)
## 11 T-CONT Containers transport
## 12 T-CONT-RL-TON Rail containers transport (weight)
## 13 T-CONT-SEA-TEU Maritime containers transport (TEU)
## 14 T-CONT-SEA-TON Maritime containers transport (weight)
```

I can now create a ggplot path plot, comparing the following categories of interest to me: – Inland road freight – Inland rail freight – Inland pipeline transport – Inland waterway transport

```
library(ggplot2)
ggplot(data_df[data_df$variable == c("T-GOODS-IW-TOT",
"T-GOODS-RD-TOT",
"T-GOODS-RL-TOT"),]) +
geom_path(mapping = aes(x=as.numeric(obsTime),y=obsValue, color=variable)) +
ggtitle("German inland freight development by considered category") +
xlab("year") +
ylab("in millions of TONNEKM")
```

The post Comparing different German transport sectors by volume, using OECD data in R appeared first on SCM data blog.

]]>The post Quadratic optimization with cvxopt in Python appeared first on SCM data blog.

]]>Using the cvxopt-module in Python I will model and solve the problem below:

In order to solve this problem using the cvxopt-module in Python, one must restate the problem in vector-matrix notation form – such that it satifies the template below:

In below coding example I solve the problem above, stating it according to the given template in vector-matrix notation form:

# preparing the modelling inputs, using cvxopt from cvxopt import matrix # setting matrices and vectors P = matrix([[2.0,0.0],[0.0,4.0]]) q = matrix([6.0,20.0]) G = matrix([[-1.0,-1.0],[-2.0,-3.0]]) h = matrix([-10.0,0.0]) # importing the solver from cvxopt-module, providing the solver function from cvxopt import solvers # using the qp-solver from cvxopt.solvers solution = solvers.qp(P=P, q=q, G=G, h=h)

pcost dcost gap pres dres 0: 3.1195e+00 1.0142e+02 2e+01 1e+00 3e-01 1: 1.1086e+02 1.1722e+02 4e+00 9e-02 2e-02 2: 1.1727e+02 1.1733e+02 4e-02 9e-04 2e-04 3: 1.1733e+02 1.1733e+02 4e-04 9e-06 2e-06 4: 1.1733e+02 1.1733e+02 4e-06 9e-08 2e-08 Optimal solution found.

# optimal x1 solution["x"][0]

4.6666665332235375

# optimal x2 solution["x"][1]

2.6666665168063117

# optimal objective function value solution["primal objective"] + 5

122.33332669148787

If interested, please go ahead and read my blog post on quadratic programming in R, using the quadprog R-package from CRAN.

The post Quadratic optimization with cvxopt in Python appeared first on SCM data blog.

]]>The post Linear optimization in Python: Using SciPy for linear programming appeared first on SCM data blog.

]]>In the SciPy-package in Python I can use the linprog function to model and solve this simple linear optimization problem. For that I will state it in vector matrix notation form – and transform it into a minimzation problem:

In Python I can solve this problem as follows:

# set up cost list with cost function coefficient values c = [-2,-3] # set up constraint coefficient matrix A A_ub = [[1,1], [2,1]] # constraint list for upper bounds (less than or equal constraints) b_ub =[10,15] # in addition, i need to prepare a bounds tuple for each optimization variable and summarize them a list x1_bounds = (0,None) x2_bounds = (0,None) # now I use SciPy.optimize.linprog to model and solve the problem at hand from scipy.optimize import linprog model_linear = linprog(c=c, A_ub=A_ub, b_ub = b_ub, bounds = [x1_bounds,x2_bounds]) # output model solution print(str(model_linear))

fun: -30.0 message: 'Optimization terminated successfully.' nit: 1 slack: array([ 0., 5.]) status: 0 success: True x: array([ 0., 10.])

Be aware, since for using linprog we transformed the problem into the form of a minimization problem the optimal objectve function value is not -30.0 but 30.0

The post Linear optimization in Python: Using SciPy for linear programming appeared first on SCM data blog.

]]>The post Applying fredr package in R: Analyzing FRED domestic car production data for USA appeared first on SCM data blog.

]]>I demonstrate the data retrieval process for FRED time series data using US domestic car production and imported car sales as an example.

First step is to setup the FRED API-key in the R script. For this you must register at FRED and copy-paste the API-key. You can do this here: **https://research.stlouisfed.org/useraccount/apikey**

```
# install fredr package directly from CRAN
#install.packages("fredr")
# load package
library(fredr)
```

`## Warning: package 'fredr' was built under R version 3.6.2`

```
# set fredr API key
fredr_set_key(api_key) # api_key string must be retrieved from https://research.stlouisfed.org/useraccount/apikey
```

Once the FRED API-key has been setup using the fredr_set_key function the fredr_series_search_text function will enable a time series search in the FRED database:

```
# search database for a car production series
search_ls <- fredr_series_search_text("car production")
# view column names of series search result list
colnames(search_ls)
```

```
## [1] "id" "realtime_start"
## [3] "realtime_end" "title"
## [5] "observation_start" "observation_end"
## [7] "frequency" "frequency_short"
## [9] "units" "units_short"
## [11] "seasonal_adjustment" "seasonal_adjustment_short"
## [13] "last_updated" "popularity"
## [15] "group_popularity" "notes"
```

The fredr_series_search_text function returns a list that summarizes the search results, providing an overview of relevant IDs and important dataset characteristics, such as e.g. seasonal adjustment.

Using the fredr_series_observations one can retrieve the desired time series data by specifying the relevant series ID. With the do.call function one can convert the returned time series list into a data frame, plotable in ggplot2:

```
# loading ggplot2 R-package
library(ggplot2)
# DAUPSA is id for seasonlly adjusted monthly domestic car production in units
series_ls <-fredr_series_observations(series_id = "DAUPSA")
# convert series list to dataframe
series_df <- do.call(cbind.data.frame, series_ls)
# plotting data
ggplot(series_df) + geom_line(mapping = aes(x=date,y=value),
color = "red") +
ggtitle("Monthly US car production, seasonally adjusted [in thousands]") +
xlab("time") +
ylab("monthly cars produced [thousands of units]")
```

Above chart summarizes US domestic car production in units, since 1993 (monthly figures, seasonally adjusted). As can be seen historical production volume has declinded.

But how about new car imports? Below I repeat above work flow for imported new car sales in billion USD, quarterly reported and seasonally adjusted:

```
# B149RC1Q027SBEA is id for US domestic sales of imported new cars, seasonally adjusted and in billions of USD
series_df <-do.call(cbind.data.frame,
fredr_series_observations(series_id = "B149RC1Q027SBEA"))
# plotting data
ggplot(series_df) + geom_line(mapping = aes(x=date,y=value),
color = "red") +
ggtitle("Quarterly US imported new car sales, seasonally adjusted [in billion USD]") +
xlab("time") +
ylab("quarterly new imported car sales [billion USD]")
```

The post Applying fredr package in R: Analyzing FRED domestic car production data for USA appeared first on SCM data blog.

]]>