Error: CausalImpact Analysis

Dear Community,

(this is my first post, just started out with R - so please forgive me for my mistakes :slight_smile: )

I am trying to do a Causal Impact Analysis with the "CausalImpact" Package. I keep running into the same error message: "data is not a numeric or integer vector"

I don't understand this, as my data is numeric... can anyone help me??

> # Synthetic control method =====================================================
> 
> #select data
> informal_small <- informal[ c("year_s", "informal_output", "carbon_tax_dummy") ] 
> informal_small$informal_output <- as.numeric(informal_small$informal_output)
Warnmeldung:
NAs durch Umwandlung erzeugt 
> informal_small %>% convert(int( "carbon_tax_dummy"), dte("year_s"))
# A tibble: 150 x 3
   year_s     informal_output carbon_tax_dummy
   <date>               <dbl>            <int>
 1 1990-01-01            35.4                0
 2 1991-01-01            35.6                0
 3 1992-01-01            35.4                0
 4 1993-01-01            35.3                0
 5 1994-01-01            35.0                0
 6 1995-01-01            34.9                0
 7 1996-01-01            34.5                0
 8 1997-01-01            34.2                0
 9 1998-01-01            33.9                0
10 1999-01-01            33.8                0
# … with 140 more rows
> 
> 
> 
> # Set pre and post intervention period
> pre.period <-  as.Date(c("1990-01-01", "2013-01-01") )
> post.period <- as.Date(c("2014-01-01", "2020-01-01") )
> 
> # Determining the causal impact
> impact <- CausalImpact(informal_small, pre.period, post.period)
Fehler: data is not a numeric or integer vector
> plot(impact)
Fehler in plot(impact) : Objekt 'impact' nicht gefunden
> 

If I am reading your code correctly pre.period and post.period are not numeric so that likely is where the problem is.

pre.period <-  as.Date(c("1990-01-01", "2013-01-01") )
str(pre.period)
 Date[1:2], format: "1990-01-01" "2013-01-01"

Hi, thanks for the reply! But this does not seem to be the problem. I find that that is how you model it with time... CausalImpact

It seems to me that somehow the data itself is not numeric..? But when I check it says it is...

I have no knowedge of CausalImpact" so I may be of little help but can you give us a reproducible example (reprex). In particular it would be good to see some sample data in dput() format. See ?dput. If you have a very large data set then something like head(dput(myfile), 100) will likely supply enough data for us to work with.

Hi, thanks for getting back to me. I actually managed to make it work. The data had too many columns as it seems. The first column "year" needed to be stored in "time.points" now it works...

# --------------------------------The dataset ---------------------------------
informal_small
# A tibble: 150 x 3
#year_s              informal_output carbon_tax_dummy
#<dttm>                        <dbl>            <dbl>
#  1 1990-01-01 00:00:00            35.4                0
#2 1991-01-01 00:00:00            35.6                0
#3 1992-01-01 00:00:00            35.4                0
#4 1993-01-01 00:00:00            35.3                0
#5 1994-01-01 00:00:00            35.0                0
#6 1995-01-01 00:00:00            34.9                0
#7 1996-01-01 00:00:00            34.5                0
#8 1997-01-01 00:00:00            34.2                0
#9 1998-01-01 00:00:00            33.9                0
#10 1999-01-01 00:00:00            33.8                0
# … with 140 more rows

# --------------------- Causal Impact Model --------------------------------

time.points <- seq.Date(as.Date("1990-01-01"), as.Date("2018-01-01"), "years")
info <- informal_small[ c( "informal_output", "carbon_tax_dummy") ] 

data <- zoo(info, time.points)
head(data)

pre.period <- as.Date(c("1990-01-01", "2012-01-01"))
post.period <- as.Date(c("2013-01-01", "2018-01-01"))

impact <- CausalImpact(data, pre.period, post.period)
plot(impact)

summary(impact)
summary(impact, "report")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.