I can't make predictions commands work because the sample always change. It also says non comparable errays/arguments when I try to multiplicate matrixs

I paste here my code. The problems begin in ##prediction section, hope someone can help. ##PREDICTION SECTION starts near the end

library(tseries)
library(moments)
library(car)
library(lmtest)
library(leaps)
library(MASS)
summary(CO2,cor=F)
attach(CO2)
names(CO2)
CO2REGRESS<- lm(AnCarb ~ Traffic + Wind + Wind^2 + sin((2 * pi)/24 * Hour) +cos((2 * pi)/24 * Hour) + sin((4 * pi)/24 * Hour) + cos((4 * pi)/24 * Hour))
attributes(CO2REGRESS)
coefficients(CO2REGRESS)
CO2REGRESS$coefficients
summary(CO2REGRESS)
plot (AnCarb ~ Traffic)
abline (CO2REGRESS$coeff[1],CO2REGRESS$coeff[2])
boxplot(CO2REGRESS$residuals)
CO2stdResiduals1<-residuals(CO2REGRESS)/sqrt(var(residuals(CO2REGRESS)))
CO2studResiduals1<-stdres(CO2REGRESS)
CO2studEsResiduals1<-studres(CO2REGRESS)
hist(CO2studResiduals1)
plot(density(CO2studResiduals1))
plot(fitted(CO2REGRESS), CO2studResiduals1)
abline (0,0)
qqnorm(CO2stdResiduals1) 
qqline(CO2stdResiduals1)
sort(CO2studResiduals1)
library(leaps)
leaps(x = cbind(Traffic, Wind, Wind^2, sin((2 * pi)/24 * Hour), cos((2 * pi)/24 * Hour), sin((4 * pi)/24 * Hour), cos((4 * pi)/24 * Hour)), y = AnCarb, nbest=2,method="adjr2")
crPlots(CO2REGRESS,id=TRUE)
CO2inf<-influence.measures(CO2REGRESS)
summary(CO2inf)
influencePlot(CO2REGRESS)
X<-cbind(Traffic, Wind, Wind^2, sin((2 * pi)/24 * Hour), cos((2 * pi)/24 * Hour), sin((4 * pi)/24 * Hour), cos((4 * pi)/24 * Hour))
X<-data.frame(X[-c(34,43,100,70),])
CO2REGRESS<-lm(X[,1] ~ X[,2]+X[,3]+X[,4])
summary(CO2REGRESS)
boxplot(CO2REGRESS$residuals)
CO2stdResiduals1<-residuals(CO2REGRESS)/sqrt(var(residuals(CO2REGRESS)))
CO2studResiduals1<-studres(CO2REGRESS)# residui studentizzati
hist(CO2studResiduals1)
plot(density(CO2studResiduals1))
plot(fitted(CO2REGRESS), CO2studResiduals1)
abline (0,0)
qqnorm(CO2stdResiduals1) 
qqline(CO2stdResiduals1)
sort(CO2studResiduals1)
library(moments)
skewness (CO2stdResiduals1)
kurtosis (CO2stdResiduals1) 
library(tseries)
jarque.bera.test(CO2stdResiduals1)
CO2Identita<-matrix(diag(1,10),nrow=10)
eigen(CO2Identita)
CO2CorX<-cor(X[,c(2,3,4)])
CO2eigenCorX<-eigen(CO2CorX)
CO2CI<-sqrt(CO2eigenCorX$values[1]/CO2eigenCorX$values[3])
vif(CO2REGRESS) 
vif(CO2REGRESS)>10 
library(car)
ncvTest(CO2REGRESS)
X<-cbind(Traffic, Wind, Wind^2, sin((2 * pi)/24 * Hour), cos((2 * pi)/24 * Hour), sin((4 * pi)/24 * Hour), cos((4 * pi)/24 * Hour))
X<-X[1:24,]
CO2REGRESS<-lm(X[,1] ~ X[,2]+X[,3]+X[,4])
spreadLevelPlot(CO2REGRESS)
X<-cbind(AnCarb^0.3592022, Traffic, Wind, Wind^2, sin((2 * pi)/24 * Hour), cos((2 * pi)/24 * Hour), sin((4 * pi)/24 * Hour), cos((4 * pi)/24 * Hour))
regressione1<-lm(X[,1] ~ X[,2]+X[,3]+X[,4])
###PREDICTION
co2train<-c(sample(1:50,20),sample(51:80,21),sample(81:57,21))
#co2train<-c(sample(1:24))
yX<-cbind(AnCarb, Traffic, Wind, Wind^2, sin((2 * pi)/24 * Hour), cos((2 * pi)/24 * Hour), sin((4 * pi)/24 * Hour), cos((4 * pi)/24 * Hour))
yXTr<-yX[co2train,]
yXTe<-yX[-co2train,]
yTe<-matrix(yXTe[,1],nrow=33)
XTe<-matrix(yXTe[,c(2,3,4)],nrow=33)
U7<-matrix(rep(1,33),nrow=33)
UXTe<-matrix(cbind(U7,XTe),nrow=33)
regressioneTr<-lm(yXTr[,1] ~ yXTr[,2]+yXTr[,3]+yXTr[,4])
summary(regressioneTr)
conf<-predict(regressioneTr, level=0.95, interval="confidence") 
Bhat<-matrix(regressioneTr$coefficients,nrow=4)
dim(Bhat)
pred<-predict(regressioneTr, newdata =XTe, interval="prediction")
ypred<-UXTe%*%Bhat
fpred<-yTe-ypred
RSSp<-sum(fpred^2)
MAPD<-sum(abs(fpred)/yTe)/33

Please read the Welcome message.
Please read the documentation of the sample and set.seed functions.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.