Skip to content

To create a Panel and run a Pooled OLS

6 messages · langensk@fas.harvard.edu, Spencer Graves, Sophie Langenskiold +2 more

#
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
#
Have you tried the following:

	OLS <- lm(Panel$y~Panel$x1+Panel$x2, data=Panel)

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:
#
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
#
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
#
On Mon, 21 Apr 2003, Sophie Langenskiold wrote:

            
I think your problem is with the merge() statement. The Panel dataframe
looks like
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

so it doesn't have X1 or Y variables.

The solution depends on what you want, but my guess is that you need
  Panel<-rbind(Cross1,Cross2)
  lm(Y~X1,data=Panel)

Or perhaps
   Panel<-rbind(cbind(Cross1,time=1),cbind(Cross2,time=2))
   lm(Y~X1,data=Panel)


	-thomas
#
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:
-----------------------------------------------------
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