Hi guys,
I am trying to do a loop for a regression model on energy data. There are columns for state, MSN(Type of energy) and the years from 1960-1990 in 5 year steps and 1991 to 2017 yearly. I tested my model for 1960 and it worked perfectly. However I have a problem doing the "for"-Loop to do the regressions on the columns/years 1961-2017. Heres some view on the Dataset itself:
Thats the problematic passage of the code with the model for 1960 working fine!
Model_1960 <- lm(log(X1960[which(MSN=="KILOM")]+1) ~ log(X1960[which(MSN=="PAACB")]+1) + log(X1960[which(MSN=="NGACB")] + X1960[which(MSN=="PQACB")] +1) + log(X1960[which(MSN=="EMACB")] + X1960[which(MSN=="ESACB")] +1) , data=Data_Total)
summary(Model_1960)
df_list = colnames(Data_Total[3:36])
for(i in df_list){
df_i = mutate(get(i), FUN)
Model_i <- lm(log(i[which(MSN=="KILOM")]+1) ~ log(i[which(MSN=="PAACB")]+1) + log(i[which(MSN=="NGACB")] + i[which(MSN=="PQACB")] +1) + log(i[which(MSN=="EMACB")] + i[which(MSN=="ESACB")] +1) , data=Data_Total)
summary(Model_i)
}
sapply(colnames(Data_Total[3:36]), function(x) {
Model <- lm(log("x"[which(MSN=="KILOM")]+1) ~ log("x"[which(MSN=="PAACB")]+1) + log("x"[which(MSN=="NGACB")] + "x"[which(MSN=="PQACB")] +1) + log("x"[which(MSN=="EMACB")] + "x"[which(MSN=="ESACB")] +1) , data=Data_Total)
summary(Model)} )
Error codes are:
> df_list = colnames(Data_Total[3:36])
>
> for(i in df_list){
+ df_i = mutate(get(i), FUN)
+ Model_i <- lm(log(i[which(MSN=="KILOM")]+1) ~ log(i[which(MSN=="PAACB")]+1) + log(i[which(MSN=="NGACB")] + i[which(MSN=="PQACB")] +1) + log(i[which(MSN=="EMACB")] + i[which(MSN=="ESACB")] +1) , data=Data_Total)
+ summary(Model_i)
+ }
Error in get(i) : object 'X1960' not found
>
> sapply(colnames(Data_Total[3:36]), function(x) {
+ Model <- lm(log("x"[which(MSN=="KILOM")]+1) ~ log("x"[which(MSN=="PAACB")]+1) + log("x"[which(MSN=="NGACB")] + "x"[which(MSN=="PQACB")] +1) + log("x"[which(MSN=="EMACB")] + "x"[which(MSN=="ESACB")] +1) , data=Data_Total)
+ summary(Model)} )
Error in "x"[which(MSN == "KILOM")] + 1 :
non-numeric argument to binary operator
Called from: eval(predvars, data, env)
I hope anybody could help me!