xuyiqing / gsynth

Generalized Synthetic Control Method
Other
135 stars 41 forks source link

plot factor loadings with only 1 treated #50

Open bjorn81 opened 4 years ago

bjorn81 commented 4 years ago

There appears to be a problem with plotting the factor loadings for a model with only 1 (<3?) treated unit.

Some function makes calls to cor.test passing the factor loadings as data. First using the control units, which works fine. But then a call is made with just the treated observations, and since there's only one observation, cor.test fails as it requires a minimum of 3 observations.

The debug is confusing me a bit, but the call to cor.test seems to be made from somewhere in the proximity of GGally::ggmatrix

Second time fails:

  plotMatrix <- list(data = data, plots = plots, title = title, 
    xlab = xlab, ylab = ylab, showStrips = showStrips, xAxisLabels = xAxisLabels, 
    yAxisLabels = yAxisLabels, showXAxisPlotLabels = showXAxisPlotLabels, 
    showYAxisPlotLabels = showYAxisPlotLabels, labeller = labeller, 
    switch = switch, xProportions = xProportions, yProportions = yProportions, 
    progress = progress, legend = legend, gg = gg, nrow = nrow, 
    ncol = ncol, byrow = byrow)
  attributes(plotMatrix)$class <- c("gg", "ggmatrix")
  plotMatrix

Example (using data from package augsynth:

df <- as.data.frame(augsynth::kansas)
library(gsynth)

gsynth_mod <- gsynth(lngdpcapita ~ treated, data = df, index = c("fips", "year_qtr"), force = "unit", 
                     se = TRUE, CV = TRUE)

plot(gsynth_mod, type = "factors")
plot(gsynth_mod, type = "loadings")
xuyiqing commented 4 years ago

Thanks. We'll look into it!

On Mon, Nov 9, 2020 at 7:13 AM bjorn81 notifications@github.com wrote:

There appears to be a problem with plotting the factor loadings for a model with only 1 (<3?) treated unit.

Some function makes calls to cor.test passing the factor loadings as data. First using the control units, which works fine. But then a call is made with just the treated observations, and since there's only one observation, cor.test fails as it requires a minimum of 3 observations.

The debug is confusing me a bit, but the call to cor.test seems to be made from somewhere in the proximity of GGally::ggmatrix

Second time fails:

plotMatrix <- list(data = data, plots = plots, title = title, xlab = xlab, ylab = ylab, showStrips = showStrips, xAxisLabels = xAxisLabels, yAxisLabels = yAxisLabels, showXAxisPlotLabels = showXAxisPlotLabels, showYAxisPlotLabels = showYAxisPlotLabels, labeller = labeller, switch = switch, xProportions = xProportions, yProportions = yProportions, progress = progress, legend = legend, gg = gg, nrow = nrow, ncol = ncol, byrow = byrow) attributes(plotMatrix)$class <- c("gg", "ggmatrix") plotMatrix

Example (using data from package augsynth:

library(data.table) library(gsynth)

DT <- as.data.table(augsynth::kansas) DT[, yearqtr := paste(year, qtr, sep = "")]

gsynth_mod <- gsynth(lngdpcapita ~ treated, data = df, index = c("fips", "year_qtr"), force = "unit", se = TRUE, CV = TRUE)

plot(gsynth_mod, type = "factors") plot(gsynth_mod, type = "loadings")

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/xuyiqing/gsynth/issues/50, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB2PKGBQ4D2GJHKVL6ZRCATSPABLFANCNFSM4TPO6ONQ .

-- Yiqing Xu

Assistant Professor Department of Political Science Stanford University http://yiqingxu.org/