Error in model.frame.default: variable lengths differ
Joran suggested to first remove the NAs before running the model. Thus, I removed the NAs, run the model and obtained the residuals. When I updated model2 by inclusion of the lagged residuals, the error message did not appear again.
Remove NAs
df2<-df1[complete.cases(df1),]
Run the main model
model2<-gam(death ~ pm10 + s(trend,k=14*7)+ s(temp,k=5), data=df2, family=poisson)
Obtain residuals
resid2 <- residuals(model2,type="deviance")
Update model2 by including the lag 1 residuals
model2_1 <- update(model2,.~.+ Lag(resid2,1), na.action=na.omit)
R - error variable lengths differ
This happens because in your first step you created a separate variable outside of your data frame, transLOT<-log(LengthofTimemin)
. When you remove a row from the data, transLOT
is unchanged. Even worse than differing lengths, your data doesn't line up any more - if the different lengths were ignored, your rows of data would be "off by one" compared to the response after the row you removed.
The simple solution is to create your transLOT
variable in the data frame. Then, whenever you do things to the data (like remove rows), the same thing is done to transLOT
.
resdata$transLOT <- log(resdata$LengthofTimemin)
Note that I also use the resdata$LengthofTimemin
rather than LengthofTimemin
which you seem to have in your workspace. Did you use attach()
at some point? You shouldn't use attach
for exactly this reason. Keep variables in the data frame!
Error in model.frame.default: variable lengths differ, R predict function
I think this has happened because you have used ov_dev$futime
and ov_dev$fustat
in your model specification rather than just using futime
and fustat
. That means that when you come to predict, the model is using the ov_dev
data for the dependent variable but ov_val
for the independent variables, which are of different length (13 versus 14). Just remove the data frame prefix and trust the data
parameter:
library(survival)
library(survminer)
library(dplyr)
# Create fake development dataset
ov_dev <- ovarian[1:13,]
# Create fake validation dataset
ov_val <- ovarian[13:26,]
# Run cox model
fit.coxph <- coxph(Surv(futime, fustat) ~ rx + resid.ds + age + ecog.ps,
data = ov_dev)
p <- log(predict(fit.coxph, newdata = ov_val, type = "expected"))
p
#> [1] 0.4272783 -0.1486577 -1.8988833 -1.1887086 -0.8849632 -1.3374428
#> [7] -1.2294725 -1.5021708 -0.3264792 0.5633839 -3.0457613 -2.2476071
#> [13] -1.6754877 -3.0691996
Created on 2020-08-19 by the reprex package (v0.3.0)
Related Topics
Text Clustering with Levenshtein Distances
Access and Preserve List Names in Lapply Function
How to Show the Y Value on Tooltip While Hover in Ggplot2
How to Add Frequency Count Labels to the Bars in a Bar Graph Using Ggplot2
Elegantly Assigning Multiple Columns in Data.Table with Lapply()
Practical Limits of R Data Frame
How to Draw the Boxplot with Significant Level
Format for Ordinal Dates (Day of Month with Suffixes -St, -Nd, -Rd, -Th)
Fill Missing Combinations in a Dataframe
How to Separate Two Plots in R
How to Select a Cran Mirror in R