What is the update() function in R?

This update() function is used to update outdated models or refit them(by default). It mainly involves getting the call, updating it, and evaluating it.

We use getCall() to extract the call in update() and other similar functions. Here, getCall() is an S3(Conventional Classes in R) generic function that returns x$call. Due to this the update() function efficiently works on new model classes.

The S3 class is one of the most prevalent classes in R programming. While an S3 object is simply a list of class attributes that is assigned an alias.

Syntax

update(old_model, new_formalae,…, evaluate = TRUE)

Parameter

It takes the following argument values.

  • old_model: The model that needs to be updated.
  • new_formula: The structure that informs how to update.
  • evaluate: it returns the fitted object when set to True. Otherwise, it updates the call.

Return value

It returns an updated call. Otherwise, when evaluate is set. It returns the fitted instance.

Explanation

# creating two numerical vectors
A <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17)
B <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03)
# it will generate some factors according to
# specified pattern of their levels
group <- gl(2, 7, 14, labels = c("A", "B"))
# combine above created vectors A & B
weight <- c(A, B)
# fit linear model
print("----------Show Linear Model----------")
linearModel <- lm(weight ~ group)
print(linearModel)
# show summary of updated linear model
print("----------Show Summary Of Model----------")
summary <- summary(updated_linearModel <- update(linearModel, . ~ . - 1))
print(summary)
# again update new linear model
print("----------Again Updated Linear Model----------")
print(update(updated_linearModel, . ~ . -1))
  • Lines 1–8: We initialize the A and B vectors.
  • Line 6: We use gl() to create 14 samples while n=2 creates 2 levels, and k=7 creates 7 replications.
  • Lines 10–12: We use lm() to fit weights, group data to a linear model, and return fitted values.
  • Lines 14–16: We use summary() to print a summary of an updated linearly fitted model on the console.
  • Lines 18 and 19: We print an updated model according to the . ~. -1 function on each value.

Output

update() function output

Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...