#Teoría lm() es la función de R para ajustar modelos lineales. Es el modelo estadístico más básico que existe y más fácil de interpretar. Para interpretarlo se usa la medida R-cuadrada, que significa qué tan cerca están los datos (va de 0 a 1, donde 1 es que el modelo explica toda la variabilidad).
# Importar base de datos
#file.choose()
base_de_datos <- read.csv("C:\\Users\\karee\\Downloads\\seguros.csv")
# Generar regresion (modelo lineal)
regresion <- lm(TotalIncurredCost ~ TotalPaid + TotalReserves + TotalRecovery + IndemnityPaid + OtherPaid + ClaimantType + AverageWeeklyWage1 + Hospital + PhysicianOutpatient + Rx, data=base_de_datos)
summary(regresion)
##
## Call:
## lm(formula = TotalIncurredCost ~ TotalPaid + TotalReserves +
## TotalRecovery + IndemnityPaid + OtherPaid + ClaimantType +
## AverageWeeklyWage1 + Hospital + PhysicianOutpatient + Rx,
## data = base_de_datos)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.175e-08 2.000e-12 1.000e-11 1.100e-11 3.301e-08
##
## Coefficients: (1 not defined because of singularities)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.563e-10 5.753e-11 -2.718e+00 0.0066 **
## TotalPaid 1.000e+00 6.737e-16 1.484e+15 <2e-16 ***
## TotalReserves 1.000e+00 5.751e-16 1.739e+15 <2e-16 ***
## TotalRecovery -1.000e+00 1.692e-14 -5.911e+13 <2e-16 ***
## IndemnityPaid 2.330e-16 1.326e-15 1.760e-01 0.8605
## OtherPaid NA NA NA NA
## ClaimantTypeMedical Only -1.194e-11 4.292e-11 -2.780e-01 0.7809
## ClaimantTypeReport Only -1.624e-12 9.111e-11 -1.800e-02 0.9858
## AverageWeeklyWage1 4.707e-15 7.490e-14 6.300e-02 0.9499
## Hospital 6.011e-18 4.123e-16 1.500e-02 0.9884
## PhysicianOutpatient 8.943e-17 7.820e-16 1.140e-01 0.9090
## Rx 2.154e-17 9.699e-16 2.200e-02 0.9823
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.142e-09 on 4785 degrees of freedom
## (26823 observations deleted due to missingness)
## Multiple R-squared: 1, Adjusted R-squared: 1
## F-statistic: 2.197e+30 on 10 and 4785 DF, p-value: < 2.2e-16
# Evaluar, y en caso necesario, ajustar la regresion
regresion <- lm(TotalIncurredCost ~ TotalPaid + TotalReserves + TotalRecovery + IndemnityPaid + OtherPaid + ClaimantType + AverageWeeklyWage1 + Hospital + PhysicianOutpatient + Rx, data=base_de_datos)
# Construir un nuevo conjunto de datos con valores para predicción
datos_nuevos <- data.frame(
TotalPaid = c(1000, 2000, 3000), # Ejemplos de valores para las variables
TotalReserves = c(500, 600, 700),
TotalRecovery = c(50, 75, 100),
IndemnityPaid = c(200, 300, 400),
OtherPaid = c(150, 250, 350),
ClaimantType = factor(c("Type1", "Type2", "Type1"), levels = levels(base_de_datos$ClaimantType)),
AverageWeeklyWage1 = c(800, 900, 850),
Hospital = c(200, 250, 300),
PhysicianOutpatient = c(100, 150, 200),
Rx = c(50, 60, 70)
)
# Realizar la predicción utilizando el modelo de regresión
predicciones <- predict(regresion, datos_nuevos)
# Ver las predicciones
predicciones
## 1 2 3
## NA NA NA
#Teoría El modelo de regresión lineal ajustado parece tener problemas de multicolinealidad o datos faltantes que están llevando a resultados inesperados, como coeficientes no definidos y predicciones nulas (NA). Esto sugiere la necesidad de revisar y posiblemente depurar las variables del modelo, así como de manejar adecuadamente los valores faltantes antes de que el modelo pueda generar predicciones útiles y confiables.