Hi guys,
I have a little issue with my code and I hope someone of you can help me.
I am trying to run a multivariate regression model through a function. The problem is that, together with the values of the coefficients, I get in my console window also the entire time series of the corresponding independent variables.
I tried to slightly modified my code to cope with it but doing so I obtain a different errors message, which i think is related to the kind of variables my code get from using read.xlsx. I already tried to use escamotages like as.vector, as.matrix etc. but they did not work.
This is my code.
###Funzione modello regressione lineare con read.xlsx###
##PLEASE CHECK##
#Il seguente codice riporta i valori dei coefficienti ma, insieme all'output di questi ultimi, riporta anche l'intero vettore della timeseries.
regressionelineare <- function (file, sheet, vardep, varindep1, varindep2) {
vardep <- read.xlsx(file = file, sheetIndex = sheet, startRow = 1, colIndex = c(1), header = TRUE)
varindep1 <- read.xlsx(file = file, sheetIndex = sheet, startRow = 1, colIndex = c(2), header = TRUE)
varindep2 <- read.xlsx(file = file, sheetIndex = sheet, startRow = 1, colIndex = c(3), header = TRUE)
lmodel<-lm(paste(vardep, "~", varindep1, "+", varindep2))
coef(lmodel)
}
regressionelineare("MLRAD.xlsx", "Sheet1",CPI,PPI,DIS) #EX COMPILAZIONE
#Il seguente codice riporta il messaggio di errore "invalid type(list) for variable 'vardep'
regressionelineare <- function (file, sheet, vardep, varindep1, varindep2) {
vardep <- read.xlsx(file = file, sheetIndex = sheet, startRow = 1, colIndex = c(1), header = TRUE)
varindep1 <- read.xlsx(file = file, sheetIndex = sheet, startRow = 1, colIndex = c(2), header = TRUE)
varindep2 <- read.xlsx(file = file, sheetIndex = sheet, startRow = 1, colIndex = c(3), header = TRUE)
modelmulti <-lm(vardep~varindep1+varindep2) # modello multivariato
summary(modelmulti)
}
regressionelineare("MLRAD.xlsx", "Sheet1",CPI,PPI,DIS) #EX COMPILAZIONE
### Procedo manualmente fuori dall'ambiente funzione: il problema รจ lo stesso, #insieme ai valori dei coefficienti
#vengono riportati anche le time series corrispondenti
CPI<-read.xlsx("MLRAD.xlsx", sheetName = "Sheet1",startRow = 1, colIndex = c(1), header = TRUE)
PPI<-read.xlsx("MLRAD.xlsx", sheetName = "Sheet1",startRow = 1, colIndex = c(2), header = TRUE)
DIS<-read.xlsx("MLRAD.xlsx", sheetName = "Sheet1",startRow = 1, colIndex = c(3), header = TRUE)
#modelmulti <-lm(CPI~PPI+DIS) # modello multivariato ### questo comando #NONFUNZIONA
lm(paste(CPI, "~", PPI, "+", DIS))
Someone can help me out?
Thank you guys