OLS模型将无法运行,并在model.frame.default中吐出Error

时间:2018-11-11 05:50:28

标签: r dataframe linear-regression economics

从上周开始,我刚刚开始用R进行编码,正在尝试建立线性模型,但是遇到了两个错误。首先,我尝试使用

进行回归
reg1 <- lm(Ex ~ Im)

Ex和Im是按国家/地区对美国的进出口,矩阵的大小完全相同。下面是显示给我的代码。

  Error in model.frame.default(formula = Ex ~ Im, drop.unused.levels = TRUE) : invalid type (list) for variable 'Ex'

然后我尝试查看是否为所有变量创建一个数据框是否可以使其工作。

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)

我的第三次尝试是合并数据集,这样我就可以运行回归qith合并数据集,我认为这可以解决问题。

IE <- merge(NAFTA, FTA, by=c(Year, Year))

R然后吐出

Error in fix.by(by.x, x) : object 'Year' not found

我不知道该怎么做才能使代码正常工作。

在下面,我放置了所有代码,以防对某人有所帮助。

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

输入NA     {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     查看数据集     {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))
``` 

0 个答案:

没有答案