Dear Luca,
If you just want to use lm() to run a weighted regression via zelig, all you need to do
is:
z.out <- zelig(formula = y ~ x1*x2 + x3 + x4 + x5, model="ls", data =
my.data,
weights = my.data$my.weights)
x.out <- setx(z.out)
Note that any arguments accepted by lm() are passed via ..., so it is not necessary to do
any recoding. zelig(...) will do everything that lm(...) can do.
You will probably need to recode your my.weights variable into a numeric variable, by
changing factor = 0 into the number of 0's in the data for example. lm() will
renormalize the weights for you, but R doesn't know what to do with a factor weight.
Best,
Olivia Lau
----- Original Message -----
From: Luca De Benedictis
To: Olivia Lau
Sent: Wednesday, April 05, 2006 8:43 AM
Subject: Re: [zelig] simulation with Zelig
Dear Olivia,
thank you for the very quick reply.
I did not use a new function, I believed that it was possible to modify the original
"ls" function extending the number of arguments in the model. Since the
"weight" argument is already present in the "lm" function I believed
it was possible to make "ls" more similar to "lm" through:
zelig2ls <- function(formula, model, data, M, ...) {
mf <- match.call(expand.dots = TRUE)
mf[[1]] <- as.name("ls")
as.call(mf)
}
In fact when I run the weighted regression the summary(z.out) gives me the standard
output.
Finally, my.weights is indeed in my.data and x1 is a categorical variable that takes
values 0,1,2,3; all other xs are categorical variables as well.
Thank you very much for your help and let me know if you want to have a look at the
dataset and the R script.
All the best
Luca
Olivia Lau wrote:
Dear Luca,
To clarify: Did you write your own "ls" function to perform the procedure
you describe below? If so, you might want to name the function something else as there is
already a Zelig model called "ls". (I suggest "wls" since your model
does something special with the weights -- you will need to change your zelig2ls function
to zelig2wls, etc.)
Can you verify that your data works with the following default syntax? I just want to
isolate the error, whether it is in your "wls" function or in our code.
First, make sure that my.weights is a variable in my.data:
my.data$my.weights
If not, insert into the data.frame:
my.data$my.weights <- my.weights
Now try:
z.out <- zelig(formula = y ~ x1*x2 + x3 + x4 + x5, model="ls", data =
my.data,
weights = my.data$my.weights)
x.out <- setx(z.out)
If that works (it should, I just tested it), then the error is likely in your wls()
function. Are you using our model.end() function prior to exiting wls()? setx() needs
some information from your data in order to run properly and model.end() codes this. If
you are using model.end(), then perhaps there is another problem and I would be happy to
take a look at your code for wls(), if you send it along.
Best,
Olivia Lau
----- Original Message -----
From: Luca De Benedictis
To: zelig(a)latte.harvard.edu
Sent: Wednesday, April 05, 2006 4:16 AM
Subject: [zelig] simulation with Zelig
Dear Zelig-list,
I am trying to use the Zelig library to simulate the following model:
z.out<-zelig(formula = y ~ x1*x2 + x3 + x4 + x5, model="ls", data =
my.data, weights = my.weights)
Since the regression is weighted before running the regression I used the
procedure:
zelig2ls <- function(formula, model, data, M, ...) {
mf <- match.call(expand.dots = TRUE)
mf[[1]] <- as.name("ls")
as.call(mf)
}
but when I set
x.out<-setx(z.out)
I receive the following error message:
Error in model.frame(formula, rownames, variables, varnames, extras, extranames, :
variable lengths differ
In addition: Warning message:
There is more than one mode. The first level is selected. in: FUN(X[[1]], ...)
Any suggestion is appreciated
Luca De Benedictis
http://www.economiamc.org/debenedictis