



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This r markdown document provides a comprehensive guide to performing regression analysis in r, focusing on calculating fitted values, residuals, and assessing model significance. It demonstrates how to interpret key statistical measures like r-squared, adjusted r-squared, and vif, and how to identify significant variables. The document also includes practical examples of how to calculate and interpret these measures, making it a valuable resource for students and researchers.
Typology: Schemes and Mind Maps
1 / 7
This page cannot be seen from the preview
Don't miss anything!




##2. Calculate Fitted Values, Residuals, and Check Results # Calculate fitted values and residuals fitted_values <- fitted(model) # Predicted values (y_hat) residuals <- resid(model) _# Residuals (y - y_hat)
results <- data.frame( Original_Y = data$Price[ 1 : 10 ], # Actual Price for the first 10 observations Fitted_Y = fitted_values[ 1 : 10 ], # Fitted values for the first 10 observations Residuals = residuals[ 1 : 10 ] # Residuals for the first 10 observations ) # Display the results print(results)
# Check if Residuals = Original_Y - Fitted_Y check <- all.equal(results$Residuals, results$Original_Y - results$Fitted_Y) print(paste("Residuals equal Original_Y - Fitted_Y: ", check))
# Extract coefficients and standard errors coefficients <- coef(model) # Estimated coefficients (β ) std_errors <- summary(model)$coefficients[, 2 ] _# Standard errors (se(β ))
calculated_t_stats <- coefficients / std_errors # Compare with t-statistics from summary() summary_t_stats <- summary(model)$coefficients[, 3 ] comparison <- data.frame( Variable = names(coefficients), Calculated_T_Stats = calculated_t_stats, Summary_T_Stats = summary_t_stats ) print(comparison)
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com (http://rmarkdown.rstudio.com). When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: # Degrees of freedom df <- model$df.residual # Critical value at 95% confidence level critical_value <- qt(0.975, df) print(paste("Critical value at 95% confidence level: ", critical_value))
Including Plots You can also embed plots, for example:
# Calculate SSE and SST SSE <- sum(residuals(model)^ 2 ) # Sum of Squared Errors SST <- sum((data$Price - mean(data$Price))^ 2 ) _# Total Sum of Squares
calculated_r_squared <- 1 - (SSE / SST) # Compare with R-squared from summary() routine_r_squared <- summary(model)$r.squared # Display comparison print(paste("Calculated R-squared: ", calculated_r_squared))
print(paste("Routine R-squared: ", routine_r_squared))
# Install and load the "car" package if (! require (car)) install.packages("car", dependencies = TRUE)
library (car) # Calculate VIF for the model vif_values <- vif(model) # Display VIF values print(vif_values)
# Check for multicollinearity (VIF > 5 or 10 indicates potential multicollinearity) multicollinearity_check <- vif_values > 5 print(multicollinearity_check)
weight_model <- lm(Weight ~ Age + KM + HP + Metallic + Automatic + CC + Doors + Gears, data = da ta) # Calculate R-squared for this regression r_squared_weight <- summary(weight_model)$r.squared print(paste("R-squared for Weight regression: ", r_squared_weight))
_#ii
vif_weight <- 1 / ( 1 - r_squared_weight) print(paste("VIF for Weight: ", vif_weight))
summary(significant_model)
Example Calculations Effect of Age: Coefficient of Age tells how much the price decreases for each additional month. Multiply by 12 to calculate the effect for one year. Effect of KM: Coefficient of KM tells how much the price decreases for every additional kilometer. Multiply by 10,000 for the impact of 10,000 km. # Retrieve coefficients coefficients_reduced <- coef(significant_model) # Effects effect_of_age <- coefficients_reduced["Age"] * 12 # Effect of 1 year older effect_of_km <- coefficients_reduced["KM"] * 10000 # Effect of 10,000 more kilometers print(paste("Price decreases by", round(effect_of_age, 2 ), "Euros for 1 year older."))
print(paste("Price decreases by", round(effect_of_km, 2 ), "Euros for 10,000 more kilometers."))