I initially put a database to a lm to perform a regression, but it believes in a function and then creates a function to predict with predict, the arguments that I put to this function are: a vector with the same structure of the initial data (I save it as a data frame and then export it as a .csv file) and regression, but when using the function it tells me that the variables have a different type, as I do so that they remain with the type of initial variable ??
predecir=function(x,regresion){
xx=read.csv2(x,sep=";",header=TRUE,dec = ",",fill=TRUE)
columns_to_change <- c(25,49:52,54:63,76)
dat[,columns_to_change]<- lapply(dat[,columns_to_change], as.factor)
xx<-data.frame(xx)
load(url(regresion))
basef1<-as.data.frame(xx[,resultado[[5]][!is.na(resultado[[5]])]])
colnames(basef1)<-resultado[[5]][!is.na(resultado[[5]])]
prediccion=predict(resultado[[1]],newdata=basef1,interval="predict",level=0.95,na.rm=FALSE)
library(xtable)
newobject<-xtable(prediccion)
print.xtable(newobject, type="html",file="resultadot1.html")
return(prediccion)
}
This is the function that I am using to predict, the file that is inserted as an argument, I change the type of variable that I had initially, for example if I had it when creating the new file and I do not take it as a factor, that's what I need to keep the types of the variables.