Hi.
In this video we will learn how to run r and Python code in the same
notebook. As we already know,
we can use R or Python kernel in the notebook, but, uh,
one kernel applies for whole notebook. In our use case,
we will have notebook with Python kernel,
but we want to make some sort of analysis in R.
This is a very effective combination because Python is a very strong programming
language, but r has many excellent statistical packages.
This functionality provides the R P Y two package,
which provides us the interface to run our embedded in a Python
process. We'll do the same chart, uh,
the scatter plot of our dependent and explanatory variable,
but in r using GG plot two library.
And the second thing, we will do the same linear regression in R.
Now please change the kernel to the base environment and, uh,
rerun the notebook.
When we want to execute our code in the cell,
we have to load the i python's r p Y two extension First,
the person sign means we are executing I python's magic command.
If you are curious about ipy magic, please check the references.
When you want to execute r code in the cell,
you have to use the R magic command at the beginning, uh,
which is the percent R or one line of code,
or twice percent and r if the whole cell will be R code.
Now let's do the chart
next to R magic command. We can input few parameters.
Most important parameters are inputs and outputs.
That means our objects,
which we would like to pass from Python to R and objects,
which we would like to pass back from R to Python. In our case,
we just need to specify the input,
and this is our data frame For our chart. Uh,
we can specify its hate and width.
We do not need to specify any output Now because we want just to plot one chart,
and this is the code to explain the code. First, we check, uh,
if GG plot two package exists. If not, it'll be installed.
And this part is the code to plot the scatter plot.
And here it is.
Please notice this data frame, which is used as input to the chart,
but in reality it is already converted to R metrics.
Now let's do the linear regression in R and compare if we will go the same
values of intercept and the coefficient.
Our input will be just our data frame, uh,
but we would like to get back from R to Python, three objects. Uh,
first will be the intercept. Second, uh, will be the coefficient,
and the last one will be the vector of our predictions.
This is the code. Let's explain it. In this line,
we create the model, uh,
where we specify our dependent variable and the explanatory variable
and also our data frame. Here in these two lines, uh,
we extract the intercept and the coefficient,
and in the last line we make the predictions. Let's run it.
We see here our information about our model.
And now let's print the variables
and let's also print, uh, the vector of predictions.
Everything seems working. Uh, now, uh,
we can see that the returned values are in a form of MPI array.
We can compare the values of the intercept and coefficient,
and we see that they are the same.
In our next video, we will learn how to download and export the notebook.