I have two variables G
and Y
that are correlated; each variable has 12 values. I compute the correlation and fit a linear regression model called rg
. Now I want to use this model to predict new values for a second variable called GP
. I want to get the Y
values that correspond with each GP
value. GP
has 5 values.
When I do the prediction I get the following error:
Warning message:
'newdata' had 5 rows but variables found have 12 rows
How can I apply the model to GP
?, Does GP
need to have 12 values? I suppose not. Is there any option in predict.lm
to do this?
G<-c(20,25,21,30,22,23,19,24,21,23,28,27)
I<-c(229,235,230,242,231,233,226,232,230,232,238,236)
#diagrama de dispersion
qqplot(G,I)
#regression
rg<-lm(I ~ G)
summary(rg)
coef(rg[1])
#coeficiente de correlación
cor(G,I)
cp<-cor(G,I,method = c("pearson"))
cs<-cor(G,I,method = c("spearman"))
# newdata
GP <- c(30,32,34,36,38)
# predecir el valor de ingresos para estos valores
X1<-data.frame(GP)
Y_pred <- predict.lm(rg,X1 )