[R] To create a Panel and run a Pooled OLS

John Fox jfox at mcmaster.ca
Mon Apr 21 05:37:47 CEST 2003


Dear Sophie,

I think that the real question here is what you're trying to do; if you 
simply want to stack one cross section on top of the other, the simplest 
way to do that is with cbind.

To understand why your merge failed, just look at the merged data frame:

     > Cross1 <- data.frame(i=c(1,2,3),Y=c(3,2,2),X1=c(2,3,4))
     > Cross2 <- data.frame(i=c(1,2,3),Y=c(2,3,1),X1=c(5,6,7))
     > Panel <- merge(Cross1,Cross2,by="i")
     > Panel
     i Y.x X1.x Y.y X1.y
     1 1   3    2   2    5
     2 2   2    3   3    6
     3 3   2    4   1    7

On the other hand, here's what you get with rbind:

     > Panel <- rbind(Cross1, Cross2)
     > Panel
     i Y X1
     1  1 3  2
     2  2 2  3
     3  3 2  4
     11 1 2  5
     22 2 3  6
     33 3 1  7

Fitting a regression to this data frame works fine; either  OLS <- 
lm(Panel$Y ~ Panel$X1)  or  OLS <- lm(Y ~ X1, data=Panel)  will do the 
trick, although the latter is clearer, I think.

I hope that this helps,
  John

At 05:02 AM 4/21/2003 +0200, Sophie Langenskiold wrote:
>I appreciate your help a lot. Unfortunately, none of your
>recommendations work. I have written a small program to illustrate my
>problems. I hope there is a solution...
>
>Cross1 <- data.frame(i=c(1,2,3),Y=c(3,2,2),X1=c(2,3,4))
>Cross2 <- data.frame(i=c(1,2,3),Y=c(2,3,1),X1=c(5,6,7))
>Panel <- merge(Cross1,Cross2,by="i")
>OLS <- lm(Panel$Y~Panel$X1)
>
>Thanks again for your help,
>Sophie
>
>-----Original Message-----
>From: Spencer Graves [mailto:spencer.graves at PDF.COM]
>Sent: den 21 april 2003 04:51
>To: langensk at fas.harvard.edu
>Cc: r-help at stat.math.ethz.ch
>Subject: Re: [R] To create a Panel and run a Pooled OLS
>
>I just see a difference between what I wrote and what I use.  Have you
>tried the following:
>
>         OLS <- lm(y~x1+x2, data=Panel)
>
>spencer graves
>###################
>If yes, could you replicate the error with a small data set that you
>send with your help request?  It can make it easier for others to
>diagnose.
>
>Hope this helps. spencer graves
>
>langensk at fas.harvard.edu wrote:
>  >
>  > Dear all,
>  >
>  > I have tried to merge two cross sections (crossA and crossB) as
>follows, both
>  > crossA and crossB include the same variables, e.g., ID,y,x1,x2:
>  >
>  > Panel <- merge(crossA,crossB,by="ID")
>  >
>  > I have, thereafter, tried to run the following pooled OLS:
>  >
>  > OLS <- lm(Panel$y~Panel$x1+Panel$x2)
>  >
>  > and have received the following error message "Error in
>model.frame(formula,
>  > rownames, variables, varnames, extras, extranames,  : invalid
>variable type"
>  >
>  > I would be very grateful for help. At the moment, I believe that I
>have tried
>  > everything.
>  >
>  > Thanks in advance,
>  > Sophie
>  >
>  > ______________________________________________
>  > R-help at stat.math.ethz.ch mailing list
>  > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help

-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox



More information about the R-help mailing list