OLS model will not run, and spits out Error in model.frame.default
up vote
1
down vote
favorite
I have just started coding in R, starting last week and am trying to make a linear model, but have run into two errors. First I tried to run a regression using
reg1 <- lm(Ex ~ Im)
Ex and Im are exports and imports to the US by country, and the matrices are exactly the same in size. Below is the code that it shows me.
Error in model.frame.default(formula = Ex ~ Im, drop.unused.levels = TRUE) : invalid type (list) for variable 'Ex'
I then tried to see if making a data frame for all the variables would allow it to work.
Im <- data.frame(Im, Im = unlist(Im), row.names = NULL)
Ex <- data.frame(Ex, Ex = unlist(Ex), row.names = NULL)
er <- data.frame(er, er = unlist(er), row.names = NULL)
FTA <- data.frame(FTA, FTA = unlist(FTA), row.names = NULL)
NAFTA <- data.frame(NAFTA, NAFTA = unlist(NAFTA), row.names = NULL)
t <- data.frame(t, t = unlist(t), row.names = NULL)
u <- data.frame(u, u = unlist(u), row.names = NULL)
d <- data.frame(D, d=unlist(D), row.names = NULL)
FBP <- data.frame(FBP, FBP = unlist(FBP), row.names = NULL)
My third attempt was to merge data sets so that I could then run a regression qith merged data sets, which I thought might fix it.
IE <- merge(NAFTA, FTA, by=c(Year, Year))
R then spit out
Error in fix.by(by.x, x) : object 'Year' not found
I have no idea what to doin order to make the code work.
Below I have put all of my code in case that is helpful for someone.
Import datasets
```r
Im = read.csv("Imports2.csv")
Ex = read.csv("Exports2.csv")
er = read.csv("erfinished2.csv")
FTA = read.csv("FTA2.csv")
NAFTA = read.csv("NAFTA2.csv")
t = read.csv("TData_US2.csv")
u = read.csv("Unemployment.csv")
D = read.csv("DtoDC.csv")
FBP = read.csv("Fiscal_Balance_US 3.csv")
```
input NAs
r
u[u == ""] <- NA
t[t == ""] <- NA
er[er == ""] <- NA
FBP[FBP == ""] <- NA
NAFTA[NAFTA == ""] <- NA
Im[Im == ""] <- NA
Ex[Ex == ""] <- NA
FTA[FTA == ""] <- NA
view dataset
r
View(Im)
View(Ex)
View(er)
View(FTA)
View(NAFTA)
View(t)
View(u)
View(D)
View(FBP)
log distance
```r
lnd = data.frame(D$Capital,log(D$Air_Distance_.miles.))
View(lnd)
ls()
```
make data frame
```r
Im <- data.frame(Im, Im = unlist(Im), row.names = NULL)
Ex <- data.frame(Ex, Ex = unlist(Ex), row.names = NULL)
er <- data.frame(er, er = unlist(er), row.names = NULL)
FTA <- data.frame(FTA, FTA = unlist(FTA), row.names = NULL)
NAFTA <- data.frame(NAFTA, NAFTA = unlist(NAFTA), row.names = NULL)
t <- data.frame(t, t = unlist(t), row.names = NULL)
u <- data.frame(u, u = unlist(u), row.names = NULL)
d <- data.frame(D, d=unlist(D), row.names = NULL)
FBP <- data.frame(FBP, FBP = unlist(FBP), row.names = NULL)
```
merge
```r
IE <- merge(NAFTA, FTA, by=c(Year, Year))
```
r dataframe linear-regression economics
add a comment |
up vote
1
down vote
favorite
I have just started coding in R, starting last week and am trying to make a linear model, but have run into two errors. First I tried to run a regression using
reg1 <- lm(Ex ~ Im)
Ex and Im are exports and imports to the US by country, and the matrices are exactly the same in size. Below is the code that it shows me.
Error in model.frame.default(formula = Ex ~ Im, drop.unused.levels = TRUE) : invalid type (list) for variable 'Ex'
I then tried to see if making a data frame for all the variables would allow it to work.
Im <- data.frame(Im, Im = unlist(Im), row.names = NULL)
Ex <- data.frame(Ex, Ex = unlist(Ex), row.names = NULL)
er <- data.frame(er, er = unlist(er), row.names = NULL)
FTA <- data.frame(FTA, FTA = unlist(FTA), row.names = NULL)
NAFTA <- data.frame(NAFTA, NAFTA = unlist(NAFTA), row.names = NULL)
t <- data.frame(t, t = unlist(t), row.names = NULL)
u <- data.frame(u, u = unlist(u), row.names = NULL)
d <- data.frame(D, d=unlist(D), row.names = NULL)
FBP <- data.frame(FBP, FBP = unlist(FBP), row.names = NULL)
My third attempt was to merge data sets so that I could then run a regression qith merged data sets, which I thought might fix it.
IE <- merge(NAFTA, FTA, by=c(Year, Year))
R then spit out
Error in fix.by(by.x, x) : object 'Year' not found
I have no idea what to doin order to make the code work.
Below I have put all of my code in case that is helpful for someone.
Import datasets
```r
Im = read.csv("Imports2.csv")
Ex = read.csv("Exports2.csv")
er = read.csv("erfinished2.csv")
FTA = read.csv("FTA2.csv")
NAFTA = read.csv("NAFTA2.csv")
t = read.csv("TData_US2.csv")
u = read.csv("Unemployment.csv")
D = read.csv("DtoDC.csv")
FBP = read.csv("Fiscal_Balance_US 3.csv")
```
input NAs
r
u[u == ""] <- NA
t[t == ""] <- NA
er[er == ""] <- NA
FBP[FBP == ""] <- NA
NAFTA[NAFTA == ""] <- NA
Im[Im == ""] <- NA
Ex[Ex == ""] <- NA
FTA[FTA == ""] <- NA
view dataset
r
View(Im)
View(Ex)
View(er)
View(FTA)
View(NAFTA)
View(t)
View(u)
View(D)
View(FBP)
log distance
```r
lnd = data.frame(D$Capital,log(D$Air_Distance_.miles.))
View(lnd)
ls()
```
make data frame
```r
Im <- data.frame(Im, Im = unlist(Im), row.names = NULL)
Ex <- data.frame(Ex, Ex = unlist(Ex), row.names = NULL)
er <- data.frame(er, er = unlist(er), row.names = NULL)
FTA <- data.frame(FTA, FTA = unlist(FTA), row.names = NULL)
NAFTA <- data.frame(NAFTA, NAFTA = unlist(NAFTA), row.names = NULL)
t <- data.frame(t, t = unlist(t), row.names = NULL)
u <- data.frame(u, u = unlist(u), row.names = NULL)
d <- data.frame(D, d=unlist(D), row.names = NULL)
FBP <- data.frame(FBP, FBP = unlist(FBP), row.names = NULL)
```
merge
```r
IE <- merge(NAFTA, FTA, by=c(Year, Year))
```
r dataframe linear-regression economics
I think it would be nice to have a better example. What doesstr(Im)
andstr(Ex)
tell you? Look athead(Im)
andhead(Ex)
. I would assume that you have to give vectors to the lm function. Something likelm(Ex$column_name ~ Im$column_name)
should work.
– JReddig
Nov 11 at 15:21
I ran it and I think you are right, it spat out: data.frame': 137088 obs. of 65 variables: $ Year : int 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 ... $ Greenland : num NA NA NA NA NA NA NA 11.6 13.1 9.4 ... $ Canada : num 69006 68253 71085 81398 87953 ... Is their a way to run a regression for all of the vectors simultaniously, instead of doing it by hand?
– Navi
Nov 11 at 15:50
1
Then try something like:lm(Ex$USA ~ Im$Mexico + Im$Canada + Im$Greenland + and so on
.
– JReddig
Nov 11 at 15:57
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have just started coding in R, starting last week and am trying to make a linear model, but have run into two errors. First I tried to run a regression using
reg1 <- lm(Ex ~ Im)
Ex and Im are exports and imports to the US by country, and the matrices are exactly the same in size. Below is the code that it shows me.
Error in model.frame.default(formula = Ex ~ Im, drop.unused.levels = TRUE) : invalid type (list) for variable 'Ex'
I then tried to see if making a data frame for all the variables would allow it to work.
Im <- data.frame(Im, Im = unlist(Im), row.names = NULL)
Ex <- data.frame(Ex, Ex = unlist(Ex), row.names = NULL)
er <- data.frame(er, er = unlist(er), row.names = NULL)
FTA <- data.frame(FTA, FTA = unlist(FTA), row.names = NULL)
NAFTA <- data.frame(NAFTA, NAFTA = unlist(NAFTA), row.names = NULL)
t <- data.frame(t, t = unlist(t), row.names = NULL)
u <- data.frame(u, u = unlist(u), row.names = NULL)
d <- data.frame(D, d=unlist(D), row.names = NULL)
FBP <- data.frame(FBP, FBP = unlist(FBP), row.names = NULL)
My third attempt was to merge data sets so that I could then run a regression qith merged data sets, which I thought might fix it.
IE <- merge(NAFTA, FTA, by=c(Year, Year))
R then spit out
Error in fix.by(by.x, x) : object 'Year' not found
I have no idea what to doin order to make the code work.
Below I have put all of my code in case that is helpful for someone.
Import datasets
```r
Im = read.csv("Imports2.csv")
Ex = read.csv("Exports2.csv")
er = read.csv("erfinished2.csv")
FTA = read.csv("FTA2.csv")
NAFTA = read.csv("NAFTA2.csv")
t = read.csv("TData_US2.csv")
u = read.csv("Unemployment.csv")
D = read.csv("DtoDC.csv")
FBP = read.csv("Fiscal_Balance_US 3.csv")
```
input NAs
r
u[u == ""] <- NA
t[t == ""] <- NA
er[er == ""] <- NA
FBP[FBP == ""] <- NA
NAFTA[NAFTA == ""] <- NA
Im[Im == ""] <- NA
Ex[Ex == ""] <- NA
FTA[FTA == ""] <- NA
view dataset
r
View(Im)
View(Ex)
View(er)
View(FTA)
View(NAFTA)
View(t)
View(u)
View(D)
View(FBP)
log distance
```r
lnd = data.frame(D$Capital,log(D$Air_Distance_.miles.))
View(lnd)
ls()
```
make data frame
```r
Im <- data.frame(Im, Im = unlist(Im), row.names = NULL)
Ex <- data.frame(Ex, Ex = unlist(Ex), row.names = NULL)
er <- data.frame(er, er = unlist(er), row.names = NULL)
FTA <- data.frame(FTA, FTA = unlist(FTA), row.names = NULL)
NAFTA <- data.frame(NAFTA, NAFTA = unlist(NAFTA), row.names = NULL)
t <- data.frame(t, t = unlist(t), row.names = NULL)
u <- data.frame(u, u = unlist(u), row.names = NULL)
d <- data.frame(D, d=unlist(D), row.names = NULL)
FBP <- data.frame(FBP, FBP = unlist(FBP), row.names = NULL)
```
merge
```r
IE <- merge(NAFTA, FTA, by=c(Year, Year))
```
r dataframe linear-regression economics
I have just started coding in R, starting last week and am trying to make a linear model, but have run into two errors. First I tried to run a regression using
reg1 <- lm(Ex ~ Im)
Ex and Im are exports and imports to the US by country, and the matrices are exactly the same in size. Below is the code that it shows me.
Error in model.frame.default(formula = Ex ~ Im, drop.unused.levels = TRUE) : invalid type (list) for variable 'Ex'
I then tried to see if making a data frame for all the variables would allow it to work.
Im <- data.frame(Im, Im = unlist(Im), row.names = NULL)
Ex <- data.frame(Ex, Ex = unlist(Ex), row.names = NULL)
er <- data.frame(er, er = unlist(er), row.names = NULL)
FTA <- data.frame(FTA, FTA = unlist(FTA), row.names = NULL)
NAFTA <- data.frame(NAFTA, NAFTA = unlist(NAFTA), row.names = NULL)
t <- data.frame(t, t = unlist(t), row.names = NULL)
u <- data.frame(u, u = unlist(u), row.names = NULL)
d <- data.frame(D, d=unlist(D), row.names = NULL)
FBP <- data.frame(FBP, FBP = unlist(FBP), row.names = NULL)
My third attempt was to merge data sets so that I could then run a regression qith merged data sets, which I thought might fix it.
IE <- merge(NAFTA, FTA, by=c(Year, Year))
R then spit out
Error in fix.by(by.x, x) : object 'Year' not found
I have no idea what to doin order to make the code work.
Below I have put all of my code in case that is helpful for someone.
Import datasets
```r
Im = read.csv("Imports2.csv")
Ex = read.csv("Exports2.csv")
er = read.csv("erfinished2.csv")
FTA = read.csv("FTA2.csv")
NAFTA = read.csv("NAFTA2.csv")
t = read.csv("TData_US2.csv")
u = read.csv("Unemployment.csv")
D = read.csv("DtoDC.csv")
FBP = read.csv("Fiscal_Balance_US 3.csv")
```
input NAs
r
u[u == ""] <- NA
t[t == ""] <- NA
er[er == ""] <- NA
FBP[FBP == ""] <- NA
NAFTA[NAFTA == ""] <- NA
Im[Im == ""] <- NA
Ex[Ex == ""] <- NA
FTA[FTA == ""] <- NA
view dataset
r
View(Im)
View(Ex)
View(er)
View(FTA)
View(NAFTA)
View(t)
View(u)
View(D)
View(FBP)
log distance
```r
lnd = data.frame(D$Capital,log(D$Air_Distance_.miles.))
View(lnd)
ls()
```
make data frame
```r
Im <- data.frame(Im, Im = unlist(Im), row.names = NULL)
Ex <- data.frame(Ex, Ex = unlist(Ex), row.names = NULL)
er <- data.frame(er, er = unlist(er), row.names = NULL)
FTA <- data.frame(FTA, FTA = unlist(FTA), row.names = NULL)
NAFTA <- data.frame(NAFTA, NAFTA = unlist(NAFTA), row.names = NULL)
t <- data.frame(t, t = unlist(t), row.names = NULL)
u <- data.frame(u, u = unlist(u), row.names = NULL)
d <- data.frame(D, d=unlist(D), row.names = NULL)
FBP <- data.frame(FBP, FBP = unlist(FBP), row.names = NULL)
```
merge
```r
IE <- merge(NAFTA, FTA, by=c(Year, Year))
```
r dataframe linear-regression economics
r dataframe linear-regression economics
asked Nov 11 at 5:50
Navi
61
61
I think it would be nice to have a better example. What doesstr(Im)
andstr(Ex)
tell you? Look athead(Im)
andhead(Ex)
. I would assume that you have to give vectors to the lm function. Something likelm(Ex$column_name ~ Im$column_name)
should work.
– JReddig
Nov 11 at 15:21
I ran it and I think you are right, it spat out: data.frame': 137088 obs. of 65 variables: $ Year : int 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 ... $ Greenland : num NA NA NA NA NA NA NA 11.6 13.1 9.4 ... $ Canada : num 69006 68253 71085 81398 87953 ... Is their a way to run a regression for all of the vectors simultaniously, instead of doing it by hand?
– Navi
Nov 11 at 15:50
1
Then try something like:lm(Ex$USA ~ Im$Mexico + Im$Canada + Im$Greenland + and so on
.
– JReddig
Nov 11 at 15:57
add a comment |
I think it would be nice to have a better example. What doesstr(Im)
andstr(Ex)
tell you? Look athead(Im)
andhead(Ex)
. I would assume that you have to give vectors to the lm function. Something likelm(Ex$column_name ~ Im$column_name)
should work.
– JReddig
Nov 11 at 15:21
I ran it and I think you are right, it spat out: data.frame': 137088 obs. of 65 variables: $ Year : int 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 ... $ Greenland : num NA NA NA NA NA NA NA 11.6 13.1 9.4 ... $ Canada : num 69006 68253 71085 81398 87953 ... Is their a way to run a regression for all of the vectors simultaniously, instead of doing it by hand?
– Navi
Nov 11 at 15:50
1
Then try something like:lm(Ex$USA ~ Im$Mexico + Im$Canada + Im$Greenland + and so on
.
– JReddig
Nov 11 at 15:57
I think it would be nice to have a better example. What does
str(Im)
and str(Ex)
tell you? Look at head(Im)
and head(Ex)
. I would assume that you have to give vectors to the lm function. Something like lm(Ex$column_name ~ Im$column_name)
should work.– JReddig
Nov 11 at 15:21
I think it would be nice to have a better example. What does
str(Im)
and str(Ex)
tell you? Look at head(Im)
and head(Ex)
. I would assume that you have to give vectors to the lm function. Something like lm(Ex$column_name ~ Im$column_name)
should work.– JReddig
Nov 11 at 15:21
I ran it and I think you are right, it spat out: data.frame': 137088 obs. of 65 variables: $ Year : int 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 ... $ Greenland : num NA NA NA NA NA NA NA 11.6 13.1 9.4 ... $ Canada : num 69006 68253 71085 81398 87953 ... Is their a way to run a regression for all of the vectors simultaniously, instead of doing it by hand?
– Navi
Nov 11 at 15:50
I ran it and I think you are right, it spat out: data.frame': 137088 obs. of 65 variables: $ Year : int 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 ... $ Greenland : num NA NA NA NA NA NA NA 11.6 13.1 9.4 ... $ Canada : num 69006 68253 71085 81398 87953 ... Is their a way to run a regression for all of the vectors simultaniously, instead of doing it by hand?
– Navi
Nov 11 at 15:50
1
1
Then try something like:
lm(Ex$USA ~ Im$Mexico + Im$Canada + Im$Greenland + and so on
.– JReddig
Nov 11 at 15:57
Then try something like:
lm(Ex$USA ~ Im$Mexico + Im$Canada + Im$Greenland + and so on
.– JReddig
Nov 11 at 15:57
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246199%2fols-model-will-not-run-and-spits-out-error-in-model-frame-default%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I think it would be nice to have a better example. What does
str(Im)
andstr(Ex)
tell you? Look athead(Im)
andhead(Ex)
. I would assume that you have to give vectors to the lm function. Something likelm(Ex$column_name ~ Im$column_name)
should work.– JReddig
Nov 11 at 15:21
I ran it and I think you are right, it spat out: data.frame': 137088 obs. of 65 variables: $ Year : int 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 ... $ Greenland : num NA NA NA NA NA NA NA 11.6 13.1 9.4 ... $ Canada : num 69006 68253 71085 81398 87953 ... Is their a way to run a regression for all of the vectors simultaniously, instead of doing it by hand?
– Navi
Nov 11 at 15:50
1
Then try something like:
lm(Ex$USA ~ Im$Mexico + Im$Canada + Im$Greenland + and so on
.– JReddig
Nov 11 at 15:57