clear **Make a dataset length of 100*** set obs 100 ***MAke a random normal variable x, and an error e*** drawnorm x, mean(4) sd(2) drawnorm e, sd(7) ***Generate a variable y that is a cubic function of x, with an error*** gen y=5*x-2*x^2+0.5*x^3+e ***Scatter y x** scatter y x ***Perform a linear regression*** regress y x predict xb0, xb ***Calculate restricted cubic splines with 1 internal knot placed at 50th centile of x*** rcsgen x, gen(splx) df(2) **Look at the knot placements*** return list **Check if the knots are placed as expected*** su x, detail **Regress y on spline terms*** regress y spl* ***Make predictions** predict xb1, xb ***Generate restricted cubic spline terms with two internal knots (equally spaced)*** rcsgen x, gen(splx2) df(3) **Check the knots*** return list **Regress and predict*** regress y splx2? predict xb2, xb **Plot results*** line xb0 xb1 xb2 x, sort|| scatter y x, msize(small) /// legend(order(1 "Linear" 2 "1 Internal Knot" 3 "2 Internal Knots"))