[R] Peculiar behaviour with MatchIt and a function

Ajay Shah ajayshah at mayin.org
Sun Apr 11 22:27:07 CEST 2010


Folks,

I have a strange situation where:

  library(MatchIt)
  f <- function(d) {
    m <- matchit(treatment ~ lsales + major.industry,
                 data=d, method="nearest", discard="hull.treat")
    treatmentfirms <- match.data(m, group="treat")
    list(m=m, treatmentfirms=treatmentfirms)
  }
  res <- f(ex)

does not work at the match.data() call, while the identical lines within f() --

  m <- matchit(treatment ~ lsales + major.industry,
               data=ex, method="nearest", discard="hull.treat")
  treatmentfirms <- match.data(m, group="treat")

work fine for the same data object.

The messages are:

  > res <- f(ex)
  Loading required package: WhatIf
  Loading required package: lpSolve
  ##  WhatIf (Version 1.5-5, built 2009-03-03)
  [1] "Preprocessing data ..."
  [1] "Performing convex hull test ..."
  [1] "Calculating distances ...."
  [1] "Note:  range of at least one variable equals zero"
  [1] "Calculating the geometric variance..."
  [1] "Calculating cumulative frequencies ..."
  [1] "Finishing up ..."
  Error in eval(expr, envir, enclos) : object 'd' not found
  Calls: f -> match.data -> eval -> eval
  Execution halted

This seems rather strange to me: code that breaks only when you embed
it into a function.

The object ex follows:

ex <- structure(list(treatment = c(FALSE, FALSE, FALSE, FALSE, TRUE,
FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE,
TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE),
lsales = c(5.20384183832515, 1.37624402526639, 2.81480973767374,
5.71903226986308, 6.29885733086709, 3.08831145484708,
5.32904053919088, 5.40033229195477, 4.35939751661352,
-0.415515443961666, 2.63476240533238, 2.82137888640921,
4.6872108963243, 4.34782331333238, 3.43237299913060, 4.26927778123189,
5.62003848122324, -1.04982212449868, 4.43378856923247,
6.08748356653522, 6.86218243368738, 5.39235400847442,
5.98085850132076, -3.50655789731998, 5.12824053617052,
7.1604809332373, 3.61818871349117, 6.8765672836593, 5.36677009218248,
5.16724010294109, 3.24259235148552, 6.81891313625405,
5.47407576620032, 6.67389197588638, 3.56416620994030,
6.61972647976091, 2.65394594210901, 5.06132840841177,
7.05999556373764, 4.44064925381964, 3.14931136148229, 2.8142103969306,
5.7288977621346, 6.60762268613979, 3.56359963768718, 6.29395517958832,
3.89222837809997, 3.83189696094886, 6.66961229653402,
6.05067640673983, 5.62905892846026, 5.19567537457616,
6.40653339135884, 7.43230035913876, 8.38195291090277, 4.3892504797632,
3.69411572090918, 3.55620483720940, 9.09292414403762,
3.60848251704191, 5.35469769998414, 6.54049594607501,
2.74791173452734, 4.74013804639858, 4.03282370557385,
3.78168650370014, 4.1293899039649, 6.07617327503494, 5.52545293913178,
4.77457535084882, 4.00878667386045, 5.39098770138934, 5.6250638899448,
4.83095025862343, 6.16695029289265, 6.39422600364183,
5.69403512320132, 6.2385199184685, 6.47216080589246, 4.06782945656635,
5.07866770448808, 6.27081828505901, 5.35983570652645,
5.15490878325268, 5.50728085428841, 4.14630430115281, 6.2271491299165,
7.47595129411925, 3.64571095871256, 6.4379596281074, 5.37184659983615,
4.59249013283072, 7.09518045678513, 4.56611745872439,
5.00609165404638, 5.945917867152, 3.35060559554610, 4.49602473872729,
3.60032142713214, 6.20776473594018, 4.9152984472697, 3.61145830714535,
7.11999156172429, 5.42899469018965, 2.75684036527164,
4.57026813397883, 4.40146154731249, 0.932164081030445,
3.15870110183213, 6.8897847723275, 4.1568497675735, 5.44410459861402,
5.04516553699308, 5.6335388212676, 8.15003491746266, 6.45397139558334,
5.07779537593878, 4.89925670611287, 5.98883609575429,
5.82688467179018, 1.17557332980424, 7.70958196126712,
7.37534349346089, 5.07072644030655, 4.62556087563583), major.industry
= structure(c(12L, 12L, 11L, 4L, 5L, 4L, 12L, 1L, 7L, 9L, 5L, 11L, 5L,
12L, 11L, 6L, 12L, 11L, 6L, 6L, 6L, 14L, 5L, 12L, 1L, 9L, 4L, 8L, 10L,
12L, 14L, 10L, 14L, 4L, 13L, 8L, 12L, 1L, 6L, 10L, 12L, 5L, 3L, 5L,
5L, 14L, 13L, 13L, 14L, 13L, 4L, 14L, 1L, 6L, 10L, 1L, 1L, 1L, 12L,
11L, 4L, 4L, 12L, 13L, 8L, 5L, 1L, 1L, 4L, 1L, 14L, 13L, 11L, 4L, 10L,
13L, 11L, 5L, 13L, 11L, 5L, 9L, 6L, 9L, 10L, 13L, 1L, 6L, 8L, 14L,
14L, 5L, 6L, 10L, 12L, 6L, 5L, 13L, 8L, 1L, 1L, 12L, 6L, 1L, 9L, 4L,
12L, 8L, 5L, 5L, 10L, 1L, 1L, 4L, 2L, 13L, 13L, 1L, 1L, 5L, 12L, 5L,
10L, 4L, 1L), .Label = c("Chemicals", "Diversified", "Electricity",
"Food", "Machinery", "Metals", "Mining", "MiscManuf", "NonMetalMin",
"Serv.Construction", "Serv.IT", "Serv.Other", "Textiles",
"TransportEq"), class = "factor")), .Names = c("treatment", "lsales",
"major.industry"), class = "data.frame", row.names = c(NA, 125L))

-- 
Ajay Shah                                      http://www.mayin.org/ajayshah  
ajayshah at mayin.org                             http://ajayshahblog.blogspot.com
<*(:-? - wizard who doesn't know the answer.



More information about the R-help mailing list