Learn R Programming

linear.tools (version 1.3.0)

get_valid_rows: identify missing rows for model/formula.

Description

identify missing rows for model/formula.

Usage

get_valid_rows(model, data)

Arguments

model
a formula or an output of lm or glm
data
the data.frame supposed to be used in modelling

Value

a boolean vector with same length as the number of rows of data, with TRUE if a row has full data for the modelling and FALSE if not.

Details

Data often contains missing values and lm() or glm() often skip those rows. This function is to identify which rows that lm() or glm() skips.

Examples

Run this code

model = lm(price ~ carat, head(ggplot2::diamonds,1000))
data = head(ggplot2::diamonds,10)

# so observation 1, 4, 7 will be not valid rows
data[c(1,4,7),"price"] = NA
data
get_valid_rows(model,data)

# error message as no "price" is found in the data
data[,"price"] = NULL
tryCatch(get_valid_rows(model,data),
         error = function(x){
           print(x)
         })

Run the code above in your browser using DataLab