time series - R - Determine goodness of fit of new data with predict function based on existing lm -
i trying apply existing model new data set. try explain example. wondering elegant way determine goodness of fit like.
basically, run regression , obtain model. summary function obtain usual output such adjusted r-squared, p-value etc.
model.lm <- lm(sepal.length ~ petal.length, data = iris[1:75,]) summary(model.lm) now want run predict function on new data , curious know how model performs on new data.
pred.dat <- predict(model.lm, newdata = iris[76:150,]) i wanted ask how can instance adjusted r-squared predicted values new data. instance, there similar summary function? ideally, find out best practice of obtaining goodness of fit of existing model based on new data looks like.
many thanks
you can translate formula of r-squared function, such as:
r_squared <- function(vals, preds) { 1 - (sum((vals - preds)^2) / sum((vals - mean(preds))^2)) } # test > r_squared(iris[76:150,]$sepal.length, pred.dat) #[1] 0.5675686 building upon function, , using correct formula can define adjusted r-squared as:
r_squared_a <- function(vals, preds, k) { 1 - ((1-r_squared(vals, preds))*(length(preds)-1))/(length(preds) - k - 1) } where k number of predictors, thus:
> r_squared_a(iris[76:150,]$sepal.length, pred.dat, 1) #[1] 0.5616448
Comments
Post a Comment