Hi all, I'm new to R and I'm trying to learn it to use with my undergraduate econometrics courses. I'm trying to work through Justin Shea's examples for Wooldridge's book, and I've hit a snag. This has to be easy, but I can't figure it out.
Using this code:
library(wooldridge)
data("hprice1")
simple_price_model<-lm(lprice~bdrms, data=hprice1)
summary(simple_price_model)
mean(lprice)
mean(lprice, data=hprice1)
I've entered two mean functions here to show what I've tried...both give the same erro.
I get results from the regression, but when I try to find the mean, or use lprice in other functions, I get an "object 'lprice' not found" error.
Obviously lprice is there as it worked in the linear model, so, there's clearly something I'm missing, but I can't find it.
The reason the mean is not working is because lprice is found within the hprice1 table. The reason it worked in the linear model is because you specified that the model should look for lprice and bdrms within the table, using data=hprice1.
See below for how to make mean work.
library(wooldridge)
data("hprice1")
# lprice is a column (variable) within the hprice1 table
# The error you see below is because R is looking for an object (vector, table) within your environment called `lprice`
mean(lprice)
#> Error in mean(lprice): object 'lprice' not found
# See the first 5 rows of data from the hprice1 table
head(hprice1)
#> price assess bdrms lotsize sqrft colonial lprice lassess llotsize
#> 1 300.000 349.1 4 6126 2438 1 5.703783 5.855359 8.720297
#> 2 370.000 351.5 3 9903 2076 1 5.913503 5.862210 9.200593
#> 3 191.000 217.7 3 5200 1374 0 5.252274 5.383118 8.556414
#> 4 195.000 231.8 3 4600 1448 1 5.273000 5.445875 8.433811
#> 5 373.000 319.1 4 6095 2514 1 5.921578 5.765504 8.715224
#> 6 466.275 414.5 5 8566 2754 1 6.144775 6.027073 9.055556
#> lsqrft
#> 1 7.798934
#> 2 7.638198
#> 3 7.225482
#> 4 7.277938
#> 5 7.829630
#> 6 7.920810
# We see here that lprice is a column within so we have to access it directly so we can pass it to the mean function
# Access columns (variables) within a table using `$`
mean(x = hprice1$lprice)
#> [1] 5.63318
# We're basically saying 'within the hprice1 table, we just want to see the lprice column'