Demo!
Using AI to translate code between R and Python
Step 2: Fit a linear model in R
In R, fitting a linear model and getting a full statistical summary takes just two lines:
Step 3: From R to Python: ask the AI
Suppose youβve never written Python before, but you need to reproduce this analysis. Letβs ask an AI assistant for help.
Prompt: βTranslate this R code to Pythonβ
π§ Prompt
Translate this R code to Python:
model <- lm(score ~ hours, data = study_data)
summary(model)
π€ AI response
In Python, the closest equivalent is scikit-learnβs LinearRegression. Hereβs the translation:
from sklearn.linear_model import LinearRegression
X = study_data[["hours"]]
y = study_data["score"]
model = LinearRegression()
model.fit(X, y)
print("Intercept:", model.intercept_)
print("Coefficient:", model.coef_[0])Try running the translated code below (the first run may take a few seconds while scikit-learn loads):
Better prompts: βTake me step by step through the differences and similarities in this codeβ
The first translation works, but a better prompt can help us actually understand what changed.
π§ Prompt
I'm a stats student fluent in R.
Translate this R code to Python using sklearn, AND:
1. Add inline comments mapping each R line to its Python equivalent.
2. Flag any idiom differences (e.g., formula interface vs. design matrix).
3. End with one concept-check question I should be able to answer
before I move on.
4. Make sure to tell me which packages have to be loaded and how to load them.
model <- lm(score ~ hours, data = study_data)
summary(model)
π€ AI response
Hereβs the same translation, broken into pieces. Click through each tab:
R: base only, no imports needed
lm() and summary() ship with base R.
Python: install and import
pip install scikit-learn numpy pandasimport numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_scoreR original
model <- lm(score ~ hours, data = study_data)
summary(model)Python (sklearn): annotated
# ββ R: model <- lm(score ~ hours, data = study_data) ββββββββββββββββββ
# R uses a formula interface: 'score ~ hours' is a symbolic expression.
# sklearn has NO formula interface. You must build X and y manually.
X = study_data[['hours']] # double brackets β 2-D DataFrame (nΓ1)
# R adds intercept automatically; sklearn does too (fit_intercept=True default)
y = study_data['score'] # single bracket β 1-D Series
model = LinearRegression() # equivalent to specifying lm(), no fitting yet
model.fit(X, y) # <- this is where lm() actually does its work
# ββ R: summary(model) βββββββββββββββββββββββββββββββββββββββββββββββββ
# R's summary() prints coefs, SEs, t-stats, p-values, F-stat, RΒ² in one call.
# sklearn gives NONE of that directly. It is a prediction library, not inference.
# You reconstruct the pieces manually:
print("Intercept :", model.intercept_) # R: coef(model)[1]
print("Coefficient:", model.coef_[0]) # R: coef(model)['hours']
print("RΒ² :", model.score(X, y)) # R: summary(model)$r.squared
# β p-values, SEs, and t-stats are NOT available in sklearn.
# Use statsmodels.formula.api.ols() if you need full inference output.Formula interface vs. design matrix. Rβs
score ~ hoursis declarative: R parses variable names, builds the design matrix, and adds an intercept for you. sklearn is imperative: you constructXas an explicit 2-D array andyas a 1-D array before fitting. If you accidentally pass a 1-D array asX, sklearn raises aValueError.
Inference vs. prediction focus. Rβs
lm()is built for statistical inference: standard errors, t-tests, F-tests are first-class output. sklearnβsLinearRegressionis optimised for predictive accuracy; it deliberately omits p-values. For inference in Python, usestatsmodels.formula.api.ols(), which also supports the formula syntax.
Attribute naming. R stores results as a named list (
model$coefficients). sklearn stores them as object attributes with trailing underscores:model.coef_andmodel.intercept_. The trailing underscore is a sklearn convention signalling βset after fitting.β
The statsmodels alternative is often the right call for real statistics.
statsmodels.formula.api.ols('score ~ hours', data=study_data).fit()accepts R-style formula syntax and produces asummary()that looks almost identical to Rβs. Use sklearn when doing prediction and cross-validation, not inference.fit_intercept=Truedefault. sklearn includes an intercept by default (same as Rβslm()). If you manually dummy-code all levels of a categorical variable and pass them in, youβll get perfect multicollinearity, the same trap as forgetting to drop a reference level in R with-1in your formula.model.score(X, y)gives RΒ², not adjusted RΒ². Rβssummary()reports both. If you need adjusted RΒ², compute it manually from n, p, and the raw RΒ².
When you write X = study_data[['hours']] with double brackets instead of study_data['hours'] with single brackets, what is the difference in shape, and why does model.fit() require that specific shape?
Step 4: Match the syntax!
Test what you just learned. For each R snippet, choose its Python equivalent.
| R | Python | |
|---|---|---|
x <- 5 |
||
c(1, 2, 3) |
||
data.frame(a = x, b = y) |
||
study_data$hours |
||
lm(y ~ x, data = df) |
||
nrow(df) |
Step 5: Your turn: fill in the blanks
Now letβs try a different task on a different dataset: the correlation between temperature and ice cream sales.
Hereβs the complete R version:
Now write the equivalent Python. Fill in the blanks below and run it!