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))
```









share|improve this question





















  • 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







  • 1




    Then try something like: lm(Ex$USA ~ Im$Mexico + Im$Canada + Im$Greenland + and so on.
    – JReddig
    Nov 11 at 15:57















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))
```









share|improve this question





















  • 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







  • 1




    Then try something like: lm(Ex$USA ~ Im$Mexico + Im$Canada + Im$Greenland + and so on.
    – JReddig
    Nov 11 at 15:57













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))
```









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 5:50









Navi

61




61











  • 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







  • 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 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


















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto