Plotting after Doing Simulation of Linear Regression with R

This line theta_hat_1000$ahat in your code does not work, because “ahat” is a rowname not a column name in the data frame. You would get the result by calling theta_hat_1000["ahat",].

However, I understand that your desired result is actually a dataframe with 3 columns (and 1000 rows) representing the 3 parameters of your regression model (intercept, x1, x2). This line in your code, coef)) produces a dataframe with 3 rows and 1000 columns. You can, for instance, transpose the matrix before changing it into a data frame to get 1000 rows and 3 columns.

theta_hat_1000 <- sapply(lms, coef)
theta_hat_1000 <-
colnames(theta_hat_1000) <- c("ahat", "x1hat", "x2hat")

       ahat     x1hat     x2hat
1 2.0259326 0.7417404 -2.111874
2 0.7827929 0.9437324 -1.944320
3 1.1034906 1.0091594 -2.035405
4 0.9677150 0.8168757 -1.905367
5 1.0518646 0.9616123 -1.985357
6 0.8600449 1.0781489 -2.017061

Now you could also call the variables with theta_hat_1000$ahat.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top