Recent Posts

More Posts

Puzzle I am not sure how do I condition on whether a value in column A appears in column B? I use the naive way value A %in% column B within a mutate function. It workers such that R would search all values in column B. What’s more, it even works within groups. However, I not sure whether it is a luck or a right way to do it? Example library(dplyr, warn.

CONTINUE READING

sandwich package is great for correcting heteroskedesticity and autocorrelation, as well as for clustering standard error. There are several instructions online talking about how to cluster standard errors in lm and plm setups. All of them mentioned that the degree of freedom needs to be corrected. However, in the newest sandwich version: 2.40, the new fucntion, vcovCL, includes the option of degree correction. Here is the change log of version 2.

CONTINUE READING

Rstudio provides view() function. However, it has some restrictions. It slows down significantly as the nunber of rows increases. In addition, the number of rows are capped by 100 in the view panel.

The office introduction of View().link

CONTINUE READING

Don’t use the same for the data.frame and variables in it.

library(data.table)
a <- data.table(a = c(1,2,3,1,2,3), b = c("a", "b", "a", "b","a", "b"))
b <- a[b == "b"]

data.table cannot distinguish which b is. b represents data.table b and the variable b in data.table a.

a[a %in% b$a]
Error in b$a : $ operator is invalid for atomic vectors

CONTINUE READING

In stata, the fixed effect model(xtreg y x1 x2 x3, fe) report a constant term. The underline estimate is \[y_{it} - \bar{yr} + \bar{\bar{y}} = a + (x_{it} - \bar{x_{i}} + \bar{\bar{x}})) \alpha + (\epsilon_{it} - \bar{e_i} + \bar{v})\] with the constrant \(\bar{v}\) equals to 0. Details can be found on Stata’s website. In R, plm package does not calculate the some whate artificial1 intercept for within models. see help(“within_intercept”, package = “plm”)↩

CONTINUE READING

R Study Notes

More Posts

This is a collection of posts related to R.

In stata, the fixed effect model(xtreg y x1 x2 x3, fe) report a constant term. The underline estimate is \[y_{it} - \bar{yr} + \bar{\bar{y}} = a + (x_{it} - \bar{x_{i}} + \bar{\bar{x}})) \alpha + (\epsilon_{it} - \bar{e_i} + \bar{v})\] with the constrant \(\bar{v}\) equals to 0. Details can be found on Stata’s website. In R, plm package does not calculate the some whate artificial1 intercept for within models. see help(“within_intercept”, package = “plm”)↩

CONTINUE READING