R Modelling (lm and Decision Trees)

Context: FIT1043_MOC · fit models in R · R’s lm = linear regression; ctree = a decision tree · lab: 30_Projects/FIT1043_Labs/Week8-R-Solution.pdf Problem it solves: fit a linear model or a decision tree to a data frame, then read its parameters/structure.

Quick Revision

  • 🎯 Trigger: model a relationship in R ➔ lm(y ~ x) for regression; ctree(y ~ features) for a tree.
  • ⚡ Key Constraint: the R formula is response ~ predictor(s) — the dependent variable goes on the left of ~.

🔧 Minimal Working Example

height <- c(151,174,138,186,128,136,179,163,152,131)
weight <- c(63,81,56,91,47,57,76,72,62,48)
fit <- lm(height ~ weight)     # fit: height = a0 + a1*weight
fit$coefficients[1]            # intercept a0  (61.38)
fit$coefficients[2]            # slope a1       (1.415)
summary(fit)                   # slope, std error, p-values, R-squared (0.9548)

Expected output: an intercept ≈ 61.38 and slope ≈ 1.415; summary() reports Multiple R-squared ≈ 0.955.

  • Fitfit <- lm(y ~ x); ~ reads “modelled by”.
  • Parametersfit$coefficients[1] = intercept, [2] = slope.
  • Diagnosticssummary(fit) → residuals, coefficient estimates, significance, and (fit quality).
  • Plot the lineplot(x, y); abline(lm(y ~ x)) overlays the fitted line.

🔀 Variations

  • Decision tree (party package)
install.packages("party"); library(party)
outputTree <- ctree(nativeSpeaker ~ age + shoeSize + score, data = inputData)
plot(outputTree)
  • Predict formlm(height ~ weight) builds a model to predict height (response) from weight (predictor).

✍️ Practice

⚠️ Common Mistakes

  • 💡 Formula directionlm(y ~ x) predicts y from x; writing it backwards fits the wrong model.
  • 💡 ctree needs the party packageinstall.packages("party") once, then library(party) each session before calling ctree.