Closed adamrauh closed 5 years ago
@shahlaebrahimi
As a general FYI, we are working to deprecate the code currently in the master branch in the very near future. We will be moving to use the code currently in the "development" branch, which is actively being worked on. Could you try installing this version of the code? I do apologize that some features (like covariate balance plots) are not fully implemented, but it would be very much appreciated if you could try your problem with the new version to help with development.
install_github("insongkim/PanelMatch", ref = "development")
Some additional questions: 1) What are your treatment, time id, and unit id variables? 2) Do you want to include lagged versions of the variable you mention in the calculations?
@adamrauh Hi
First, I thank you very much for your time and consideration. Then, answers to your questions: 1-The variable with the title "switch" is used as treatment. The variable named "date" is considered as time id. Besides, unit id is shown by "id". 2-No.
In the first stage, I should match treated and control companies using the variables "big4", "lnasset", "leverage", and "loss".
'switch =big4+ lnasset+ leverage+ loss'
In the second stage, I should do difference in difference on the previous stage matched sample using the following equation:
'decost= switch+ post_switch +switch*post_switch+ lnaudten +big4 +altmanz +lnasset +lnage +markettobook+ leverage +profit+ tangible+ cashvol'
However, since the treatment dates are different for each firm and the treatment could occur more than once for each firm, I don’t know how to define "post_switch".
Thanks in advance. Best regards,
@adamrauh Sorry, I uploaded the dataset here: https://www.dropbox.com/s/ujteiwp0jqrtip4/psm1.xlsx?dl=0
I want to know if auditor switching affects the cost of debt. In other words, if the debt cost of companies after switching would be greater than before for companies which have switched their auditors? That's why I first want to match switched and control companies. Then, I want to use diff in diff model. Thanks in advance. Best regards,
Sorry for the delay in getting back to you. Were you able to install the development branch? If you're able to get that operational-- in order to produce a matched/refined set, try the following code:
PanelMatch2(lag = XX, time.id = "dateid", unit.id = "crossid", treatment = "switch", outcome = "decost", refinement.method = XX,
size.match = XX, data = XX, covs.formula = ~ big4 + lnasset + leverage + loss)
Of course, fill in the appropriate parameters for the XXs as desired (so 'ps.match' or 'ps.weight' for refinement.method, etc.) We've temporarily renamed PanelMatch to PanelMatch2. This will change back eventually.
I apologize again for the incomplete documentation. I'm currently working on implementing some remaining features before circling back to that. If the PanelMatch2 call works, we can discuss how to obtain point estimates.
@adamrauh First, I am so grateful for your time and consideration. Then, you're welcome. Sorry, this error is reported while I tried:
install_github("insongkim/PanelMatch", ref = "development") Error in i.p(...) : (converted from warning) installation of package ‘C:/Users/Markazi.co/AppData/Local/Temp/RtmpAtViSu/file1c30cb33360/PanelMatch_0.0.1.tar.gz’ had non-zero exit status
Thanks in advance. Best regards,
@shahlaebrahimi interesting, I haven't seen this before. Could you post what OS you are using and perhaps a bit more of the error message if possible?
@adamrauh Hi. I am working on Windows. The previous error was related to my Windows 10. However, I also tried it on Windows 7:
> library(devtools)
Warning messages:
1: package ‘devtools’ was built under R version 3.5.2
2: package ‘usethis’ was built under R version 3.5.2
>
> install_github("insongkim/PanelMatch", ref = "development")
Downloading GitHub repo insongkim/PanelMatch@development
√ checking for file 'C:\Users\t\AppData\Local\Temp\Rtmp2hWqu5\remotes1a086a5b4e2d\insongkim-PanelMatch-192ce26/DESCRIPTION' ...
- preparing 'PanelMatch':
√ checking DESCRIPTION meta-information ...
- cleaning src
- checking for LF line-endings in source and make files and shell scripts
- checking for empty or unneeded directories
- looking to see if a 'data/datalist' file should be added
- building 'PanelMatch_0.0.1.tar.gz'
* installing *source* package 'PanelMatch' ...
** libs
C:/RBuildTools/3.5/mingw_32/bin/g++ -I"C:/PROGRA~1/R/R-35~1.1/include" -DNDEBUG -I"C:/Program Files/R/R-3.5.1/library/RcppArmadillo/include" -I"C:/Program Files/R/R-3.5.1/library/Rcpp/include" -I"C:/Program Files/R/R-3.5.1/library/RcppEigen/include" -O2 -Wall -mtune=generic -c PanelEstimate_helpers.cpp -o PanelEstimate_helpers.o
In file included from C:/RBuildTools/3.5/mingw_32/i686-w64-mingw32/include/c++/unordered_map:35:0,
from PanelEstimate_helpers.cpp:3:
C:/RBuildTools/3.5/mingw_32/i686-w64-mingw32/include/c++/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
^
PanelEstimate_helpers.cpp: In function 'Rcpp::NumericVector get_vit_index(Rcpp::CharacterVector, Rcpp::CharacterVector, Rcpp::NumericVector)':
PanelEstimate_helpers.cpp:11:3: error: 'unordered_map' is not a member of 'std'
std::unordered_map<std::string, int> indexMap;
^
PanelEstimate_helpers.cpp:11:3: note: suggested alternative:
In file included from C:/RBuildTools/3.5/mingw_32/i686-w64-mingw32/include/c++/tr1/unordered_map:42:0,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/Rcpp/platform/compiler.h:172,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/Rcpp/r/headers.h:59,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/RcppCommon.h:29,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/Rcpp.h:27,
from PanelEstimate_helpers.cpp:1:
C:/RBuildTools/3.5/mingw_32/i686-w64-mingw32/include/c++/tr1/unordered_map.h:180:11: note: 'std::tr1::unordered_map'
class unordered_map
^
PanelEstimate_helpers.cpp:11:33: error: expected primary-expression before ',' token
std::unordered_map<std::string, int> indexMap;
^
PanelEstimate_helpers.cpp:11:35: error: expected primary-expression before 'int'
std::unordered_map<std::string, int> indexMap;
^
PanelEstimate_helpers.cpp:16:5: error: 'indexMap' was not declared in this scope
indexMap[key] = i + 1;
^
PanelEstimate_helpers.cpp:23:15: error: 'indexMap' was not declared in this scope
int idx = indexMap[key];
^
PanelEstimate_helpers.cpp: In function 'Rcpp::List re_norm_index(Rcpp::NumericMatrix, Rcpp::NumericVector, Rcpp::NumericVector, int, Rcpp::List, Rcpp::NumericVector)':
PanelEstimate_helpers.cpp:94:2: error: 'unordered_map' is not a member of 'std'
std::unordered_map<int, int> rowmap;
^
PanelEstimate_helpers.cpp:94:2: note: suggested alternative:
In file included from C:/RBuildTools/3.5/mingw_32/i686-w64-mingw32/include/c++/tr1/unordered_map:42:0,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/Rcpp/platform/compiler.h:172,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/Rcpp/r/headers.h:59,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/RcppCommon.h:29,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/Rcpp.h:27,
from PanelEstimate_helpers.cpp:1:
C:/RBuildTools/3.5/mingw_32/i686-w64-mingw32/include/c++/tr1/unordered_map.h:180:11: note: 'std::tr1::unordered_map'
class unordered_map
^
PanelEstimate_helpers.cpp:94:21: error: expected primary-expression before 'int'
std::unordered_map<int, int> rowmap;
^
PanelEstimate_helpers.cpp:97:3: error: 'rowmap' was not declared in this scope
rowmap[compmat_row_units[i]] = i;
^
PanelEstimate_helpers.cpp:99:2: error: 'unordered_map' is not a member of 'std'
std::unordered_map<int, int> colmap;
^
PanelEstimate_helpers.cpp:99:2: note: suggested alternative:
In file included from C:/RBuildTools/3.5/mingw_32/i686-w64-mingw32/include/c++/tr1/unordered_map:42:0,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/Rcpp/platform/compiler.h:172,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/Rcpp/r/headers.h:59,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/RcppCommon.h:29,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/Rcpp.h:27,
from PanelEstimate_helpers.cpp:1:
C:/RBuildTools/3.5/mingw_32/i686-w64-mingw32/include/c++/tr1/unordered_map.h:180:11: note: 'std::tr1::unordered_map'
class unordered_map
^
PanelEstimate_helpers.cpp:99:21: error: expected primary-expression before 'int'
std::unordered_map<int, int> colmap;
^
PanelEstimate_helpers.cpp:102:3: error: 'colmap' was not declared in this scope
colmap[compmat_cols[i]] = i + 1;
^
PanelEstimate_helpers.cpp:112:21: error: 'colmap' was not declared in this scope
int st_year_col = colmap[key];
^
PanelEstimate_helpers.cpp:118:14: error: 'rowmap' was not declared in this scope
int idx = rowmap[key];
^
PanelEstimate_helpers.cpp: In function 'Rcpp::LogicalVector check_treated_units(Rcpp::NumericMatrix, Rcpp::NumericVector, Rcpp::NumericVector, int, Rcpp::NumericVector, Rcpp::NumericVector)':
PanelEstimate_helpers.cpp:168:2: error: 'unordered_map' is not a member of 'std'
std::unordered_map<int, int> rowmap;
^
PanelEstimate_helpers.cpp:168:2: note: suggested alternative:
In file included from C:/RBuildTools/3.5/mingw_32/i686-w64-mingw32/include/c++/tr1/unordered_map:42:0,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/Rcpp/platform/compiler.h:172,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/Rcpp/r/headers.h:59,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/RcppCommon.h:29,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/Rcpp.h:27,
from PanelEstimate_helpers.cpp:1:
C:/RBuildTools/3.5/mingw_32/i686-w64-mingw32/include/c++/tr1/unordered_map.h:180:11: note: 'std::tr1::unordered_map'
class unordered_map
^
PanelEstimate_helpers.cpp:168:21: error: expected primary-expression before 'int'
std::unordered_map<int, int> rowmap;
^
PanelEstimate_helpers.cpp:171:3: error: 'rowmap' was not declared in this scope
rowmap[compmat_row_units[i]] = i;
^
PanelEstimate_helpers.cpp:173:2: error: 'unordered_map' is not a member of 'std'
std::unordered_map<int, int> colmap;
^
PanelEstimate_helpers.cpp:173:2: note: suggested alternative:
In file included from C:/RBuildTools/3.5/mingw_32/i686-w64-mingw32/include/c++/tr1/unordered_map:42:0,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/Rcpp/platform/compiler.h:172,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/Rcpp/r/headers.h:59,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/RcppCommon.h:29,
from C:/Program Files/R/R-3.5.1/library/Rcpp/include/Rcpp.h:27,
from PanelEstimate_helpers.cpp:1:
C:/RBuildTools/3.5/mingw_32/i686-w64-mingw32/include/c++/tr1/unordered_map.h:180:11: note: 'std::tr1::unordered_map'
class unordered_map
^
PanelEstimate_helpers.cpp:173:21: error: expected primary-expression before 'int'
std::unordered_map<int, int> colmap;
^
PanelEstimate_helpers.cpp:176:3: error: 'colmap' was not declared in this scope
colmap[compmat_cols[i]] = i + 1;
^
PanelEstimate_helpers.cpp:184:21: error: 'colmap' was not declared in this scope
int st_year_col = colmap[key];
^
PanelEstimate_helpers.cpp:186:13: error: 'rowmap' was not declared in this scope
int idx = rowmap[key];
^
make: *** [C:/PROGRA~1/R/R-35~1.1/etc/i386/Makeconf:215: PanelEstimate_helpers.o] Error 1
ERROR: compilation failed for package 'PanelMatch'
* removing 'C:/Program Files/R/R-3.5.1/library/PanelMatch'
* restoring previous 'C:/Program Files/R/R-3.5.1/library/PanelMatch'
In R CMD INSTALL
Error in i.p(...) :
(converted from warning) installation of package ‘C:/Users/t/AppData/Local/Temp/Rtmp2hWqu5/file1a0852deadb/PanelMatch_0.0.1.tar.gz’ had non-zero exit status
In addition: Warning messages:
1: In untar2(tarfile, files, list, exdir) :
skipping pax global extended headers
2: In untar2(tarfile, files, list, exdir) :
skipping pax global extended headers
Sorry, now I don't have access to my Windows 10 but I will post it's full error too. Thanks in advance. Best regards,
Looks like this was actually a pretty important bug to find. For some reason, there were issues getting c++11 features (needed for some internal functions) with the default configuration of Rtools/Rcpp on Windows. I was able to replicate it on a windows system on my end. I was able to get around the issue by adding c++11 as an explicit system requirement in the DESCRIPTION
file. I created a separate branch for working out the problem. Could you try installation with the following:
install_github("insongkim/PanelMatch", ref = "issue_21")
@adamrauh Hi I thank you very much for sharing your time and knowledge. Fortunately, I could manage to install it. However, this error is reported:
> library(readxl)
> library(PanelMatch)
>
> df= read_excel("PSM.xlsx",col_types = "numeric")
There were 50 or more warnings (use warnings() to see the first 50)
>
> PanelMatch2(lag = XX, time.id = "dateid", unit.id = "crossid", treatment = "switch", outcome = "decost", refinement.method = XX,
+ size.match = XX, data = df, covs.formula = ~ big4 + lnasset + leverage + loss)
Error: Can't find columns `switch`, `decost` in `.data`.
Call `rlang::last_error()` to see a backtrace
>
I would appreciate if you could let me know what should be written instead of XX for "lag", "size. match" and "refinement.method"?
I need to compare "decost" one year before "switch" with one year after it.
Besides, could you please let me know how to define "post_switch" in the diff in diff stage? It's coefficient should be reported.
decost= switch+ post_switch +switch*post_switch+ lnaudten +big4 +altmanz +lnasset +lnage +markettobook+ leverage +profit+ tangible+ cashvol
Thanks in advance. Best regards,
I was able to get the first part of your problem to run with the following code. Some notes:
Right now, its required that the columns for date and unit id be integer, which is why I'm explicitly setting that. You also need to make sure that the names you specify in the PanelMatch call match exactly to the names of the columns in the original data. I took out the spaces and lowercased the names of the columns to make that easier.
The refinement.method argument specifies which method you want to use in refining your matched sets. You can choose between: "ps.weight", "ps.match", "mahalanobis", "CBPS.weight", "CBPS.match" These will use standard propensity scores (first two methods), mahalanobis distance, or covariate balanced propensity scores (last two methods) to find similar control units. Matching methods will use that + the size.match argument to generate refined matched sets. Weighting methods will assign weights to each unit in a set of controls.
The "lag" and "size.match" arguments are just parameters that will affect your matched sets. Lag sets the size of the treatment history window you want to use for matching. Size.match sets the maximum size of the resulting matched sets if using a matching method, as mentioned before.
df <- read_excel("psm1.xlsx", col_types = "numeric")
df<-as.data.frame(df)
df$dateid <- as.integer(df$dateid)
df$crossid <- as.integer(df$crossid)
colnames(df) <- gsub(" ", "", tolower(colnames(df)))
gh.match <- PanelMatch2(lag = 4, time.id = "dateid", unit.id = "crossid", treatment = "switch", outcome = "decost", refinement.method = "ps.weight", data = df, covs.formula = ~ big4 + lnasset + leverage + loss)
I think the estimation step will look something like:
PanelEstimate2(lead = 0:1, inference = "bootstrap", estimator = "did", sets = gh.match, data = df, outcome.variable = "decost")
@adamrauh Hi. I am really indebted to you for your time and consideration. Sorry, this error is thrown out:
Error in if (qoi == "att" | qoi == "ate") { : argument is of length zero
I would appreciate if you could let me know how to graphically or statistically evaluate whether the estimated propensity score leads to balance between treatment and control observations? Does the firs column of gh.match show the matched observation? Are 94 observations matched?
Thanks in advance. Best regards,
@adamrauh Sorry, although I read the following posts I don't know how to define the variable named post_switch
because firms are treated at different time periods. Besides, firms maybe treated more than once.
The last answer by Enrique Pinzon here:
https://www.statalist.org/forums/forum/general-stata-discussion/general/1309504-difference-in-difference-on-panel-data-without-treatment-and-control-group-distinction
Thanks in advance. Best regards,
@shahlaebrahimi I'm not entirely sure what you mean by defining post_switch
. The PanelMatch and PanelEstimate functions will handle the fact that firms are treated at different time periods and/or more than once. That is something that the method allows for, so I don't think there is any need to define a new variable.
There is a get_covariate_balance function that was added to check balance, but it hasn't been tested much yet, so it might be buggy. If you update to the latest version of the development branch, you should have access to it, although I haven't written any documentation on it yet. I might be getting around to that today.
I was able to get this to run on the latest version of the development branch (I have since modified the function names to be PanelMatch and PanelEstimate rather than PanelMatch2/PanelEstimate2)
gh.est <- PanelEstimate(lead = 0:1, inference = "bootstrap", estimator = "did", sets = gh.match, data = df, outcome.variable = "decost", qoi = "att")
you can plot these results with plot(gh.est)
Regarding the output of the PanelMatch function, take a look at the part of the documentation detailing "matched.set" objects. Hopefully that clears some things up, but if not, let me know. I need to make sure that documentation is clear for future users, so any feedback you have is welcome.
@adamrauh Hi. Thank you very much for your time and patience. Sorry, now this error is reported:
> gh.est <- PanelEstimate(lead = 0:1, inference = "bootstrap", estimator = "did", sets = gh.match, data = df, outcome.variable = "decost", qoi = "att") Error in PanelEstimate(lead = 0:1, inference = "bootstrap", estimator = "did", : could not find function "PanelEstimate"
Probably it is due to the fact that I installed the package using:
install_github("insongkim/PanelMatch", ref = "issue_21")
If I try the previous instruction i.e.,
install_github("insongkim/PanelMatch", ref = "development")
that old error is again reported i.e.,
Error in i.p(...) : (converted from warning) installation of package ‘C:/Users/Markazi.co/AppData/Local/Temp/Rtmp4UpVe2/filee207292475d/PanelMatch_0.0.1.tar.gz’ had non-zero exit status
With respect to post-switch
, as I read,
"for a (basic) DiD analysis, you need two dummy variables. The first identifies the treatment group (1 if the unit belongs to the treatment group, 0 otherwise). I call this variable treatment below. The second dummy identifies the post-treatment period (1 after the event, 0 before). This variable I call post below. Then, you interact both variables with each other. Since, multiplying something with 0 yields always 0, the interaction will only be 1 for treatment units after the event."
so, in my dataset, the first dummy variable is switch
but I don't know how to define the second dummy, which I call it post-switch
. I need these two dummy variables as well as their interactions to be included in the diff-in-diff model. In fact, their coefficients should be interpreted.
By additional tests, I mean something like t-test and common support graph which are provided on the following document (pages 14-17 ). https://edge.edx.org/assets/courseware/v1/b8d2a8030b7aa5f2762a464bf7f8b0c7/c4x/BerkeleyX/CEGA101AIE/asset/Module_2.5_Difference_in_Differences.pdf
I would appreciate if you could let me know how I could have access the documentation of PanelMatch.
Thanks in advance. Best regards,
I was able to install the development branch of the code on a Windows 10 machine...that should work on your end. I've updated the function names in this version to remove the "2"s as well. Once you have the package installed, you can view the documentation for a particular function by typing ?PanelMatch
(or whatever the appropriate function is). As I understand it, you don't need to create a second treatment variable as you describe. Either that, or PanelMatch is not appropriate for what you need. I suggest consulting the original paper describing the method and looking at the example data set provided (called dem
) in the package. Here is a link to the paper
@adamrauh Hi. I thank you very much for all your efforts and time. Sorry for being late to response. Best regards,
@shahlaebrahimi let me know if you need other assistance -- happy to open another issue or talk via email
"@adamrauh Hi
I would greatly appreciate if you could answer this question: https://stats.stackexchange.com/questions/388968/how-to-do-psm-with-panel-data-using-panelmatch Thanks in advance. Best regards,"