# logistic regression raster error

I am trying to run a code in R but I get an error. I am conducting a logistic regression in order to predict a raster by another raster. the code is as follows:

``````> sinks <- raster("rasterized.tif")
> tpi <- raster("TPI.tif")
> model <- glm(sinks[]~tpi[], family= binomial)
> summary(model)

Call:
glm(formula = sinks[] ~ tpi[], family = binomial)

Deviance Residuals:
Min       1Q   Median       3Q      Max
-0.5368  -0.3780  -0.3774  -0.3766   2.3877

Coefficients:
Estimate Std. Error   z value Pr(>|z|)
(Intercept) -2.606015   0.001022 -2551.117   <2e-16 ***
tpi[]       -0.321924   0.038967    -8.261   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 7496150  on 14968071  degrees of freedom
Residual deviance: 7496082  on 14968070  degrees of freedom
(15480 observations deleted due to missingness)
AIC: 7496086

Number of Fisher Scoring iterations: 5}
``````

But then I try to use the model for another raster(tpi_test), but I get the following error:

``````>test <- raster("tpi_test.tif")
> predict(test, model, type="response")
Error in p[-naind, ] <- predv :
number of items to replace is not a multiple of replacement length
'newdata' had 690549 rows but variables found have 14983552 rows
``````

I am very new to R. Can anybody tell me what the problem is?

You've got `predict` arguments the wrong way round, and also you should really fit to a data frame, otherwise odd things happen. I mean:

Make a data frame from your data:

``````> rdata =data.frame(sinks=sinks[], tpi=tpi[])
``````

Fit the model to the data:

``````> model = glm(sinks~tpi, data=rdata, family=binomial)
``````

Predict by creating a new data frame with the right column name:

``````> predict(model, newdata=data.frame(tpi=test[]))
1         2         3         4         5
0.9165902 0.6661829 0.5761266 0.7739644 0.8667148
``````