3 Notes of R and Python
3.2 Conda environment
To begin, I will first create an environment that would contain all the python modules of the entire Social Network Science Project using conda. After installing conda in my computer, I open my terminal and add the following line of codes: conda create --name sns python=3.8 anaconda
that would create a new environment called sns
(Social Network Science) and would also install python version 3.8.
3.4 Connecting R and Python
We can run Python
codes directly from R
using RMarkdown
. I prefer connecting the best of both worlds. Mostly, because in my personal workflow I tend to use statistical models for social networks analysis. Some of these other statistical models that use social networks are:
Different models available in Statnet (e.g. exponential random graph models, epidemiological models, relational event models, or the latent position and cluster models for statistical networks)
Now that I have an environment called sns
, I could now start using R and Python together. We would need to install the reticulate
package.
#install.packages("reticulate")
library(reticulate)
use_condaenv(condaenv = "sns", conda = "auto", required = TRUE)
<- import_main() main
We can also install a package from R using anaconda of Python, and run any Python code in R using py_run_string
#reticulate::conda_install(c("leidenalg", "igraph"),
# envname = "sns", pip = TRUE)
py_run_string("import numpy as np")
py_run_string("my_python_array = np.array([2,4,6,8])")
py_run_string("print(my_python_array)")
Also, we can use python directly from Rmarkdown
adding the chunk {python}
instead of {r}
in the Rmarkdown. For example, we can replicate the classic model of Holland, Laskey & Leinhardt (1983) on Stochastic Block Model using stochastic_block_model
import networkx as nx
= [75, 75, 300]
sizes = [[0.25, 0.05, 0.02],
probs 0.05, 0.35, 0.07],
[0.02, 0.07, 0.40]]
[
= nx.stochastic_block_model(sizes, probs, seed=0)
g len(g)
= nx.quotient_graph(g, g.graph['partition'], relabel=True)
H
for v in H.nodes(data=True):
print(round(v[1]['density'], 3))
for v in H.edges(data=True):
print(round(1.0 * v[2]['weight'] / (sizes[v[0]] * sizes[v[1]]), 3))
There are neat packages out there in Python
, some of my favourites are:
python
: general purpose network analysisNetworkX
: general purpose network analysispyintergraph
: convert Python-Graph-Objects between networkx, python-igraph and graph-toolssnap
: general purpose network analysismetaknowledge
: computational research in bibliometrics, scientometrics, and network analysispySciSci
: computational research in bibliometrics, scientometrics, and network analysisleiden_lag
: for community detection, with a special focus in theleiden algorithm
. Some nice codes are available in the official GitHub of CWTSgraph-tool
: for stochastic block models and other general purpose network analysis. Check also the databases available in Netzschleuder.EstimNetDirected
: Exponential random graph models for big networksALAAMEE: Autologistic Actor Attribute Model (ALAAM) parameter estimation using Equilibrium Expectation (EE) algorithm