Watts-College / cpp-523-sum-2022

https://watts-college.github.io/cpp-523-sum-2022/
0 stars 2 forks source link

Plotting quadratic model #10

Open varvel opened 2 years ago

varvel commented 2 years ago

@JasonSills In the Lab 06 instructions there is code to plot the Income Happiness data. How did you define the object y_hat to produce the line here?

lines( 1:200000, y_hat, col=gray(0.3,0.5), lwd=6 )

JasonSills commented 2 years ago

@varvel,

yhat is the line in the graph. It's in the form of a quadratic, so it's not as simple as plotting the regression line. The code chunk below will need to come before or be added. We have two variables in the data: happiness and income. We see it is a quadratic so we need to square income and add it in. There is an additional line of code required that sets the length of x and y, otherwise you will receive an error.

Drop this in and you should be able to recreate the lab instructions:

dat$income2 <- dat$income^2
m1 <- lm( happiness ~ income + income2, data=dat )
y_hat <- predict( m1, data.frame( income=1:200000, income2=(1:200000)^2 ) )
varvel commented 2 years ago

Thank you!