p1
unknown
r
4 years ago
5.4 kB
4
Indexable
dane <- readRDS("cwiczenia_3/dane/dane2007.rds") write.csv(dane, "dane.csv", row.names=FALSE) names(dane) library(ggpubr) library(car) library(dplyr) library(ggplot2) library(ggResidpanel) ??car # usunąć - date, mce oraz TwMP dane = dane %>% select(-c(data, mce, TwMP)) dane dane = dane[complete.cases(dane),] png("zad1_a.png") ggplot(dane, aes(x=TwP, y=tps)) + geom_point() + xlab("TwP: Temperatura wody w Parsęcie [C]") + ylab("tps: Temperatura powietrza [C]") + geom_smooth() + geom_abline(color = 'grey') + theme_bw() dev.off() model_lm <- lm(formula = TwP ~ tps, data = dane) write.csv(model_lm$coefficients, "zad1_wspolczynniki.csv") model_lm$coefficients # współczynnik modelu summary(model_lm) # TwP = 0.62243 * tps - 3.08151 new <- data.frame(tps = c(5.3, 2.1, 21)) p <- predict(model_lm, new) p #oszacowana temperatura png("zad1_b.png") ggplot(dane, aes(x=TwP, y=tps)) + geom_point() + xlab("TwP: Temperatura wody w Parsęcie [C]") + ylab("tps: Temperatura powietrza [C]") + geom_smooth() + geom_abline(color = 'grey') + theme_bw() + stat_regline_equation(data = dane, aes(x=TwP, y=tps), formula = x ~ y, show.legend = TRUE, label.x = 0, label.y = 25) + stat_cor(data = dane, x.label = 15, label.y = 20) dev.off() model_df <- model_lm$model model_df$oszacowane = model_lm$fitted.values model_df$reszty = model_lm$residuals model_lm png("zad1_c.png") ggplot(model_df, aes(x=TwP, y=oszacowane)) + geom_point() + xlab("Wartości obserwowane") + ylab("Wartości dopasowane") + geom_smooth() + geom_abline(color = 'grey') + theme_bw() dev.off() smr_model_lm <-summary(model_lm) smr_model_lm$r.squared # determinacji # 91,59 % zmienności zmiennej TwP jest wyjaśniona przez zmienność tps outlierTest(model_lm) #odstające id 120 dane[120, ] png("zad1_d.png") ggplot(dane, aes(x=TwP, y=tps)) + geom_point() + geom_point(data = dane[120,], aes(x=TwP, y=tps), color = "red", size = 4) + xlab("TwP: Temperatura wody w Parsęcie [C]") + ylab("tps: Temperatura powietrza [C]") + geom_smooth() + geom_abline(color = 'grey') + theme_bw() dev.off() summary(smr_model_lm$residuals) # reszty z modelu png("zad1_e.png") resid_panel(model_lm) # wykresy reszt dev.off() #--------- # POSTĘPUJĄCA start_m <- lm(TwP ~ 1, data = dane) #model tylko z wyrazem wolnym end_m <- lm(TwP ~ ., data = dane) #model ze wszystkimi zmiennymi w zbiorze danych model_koniec_1 <- step(start_m, scope=list(lower=start_m, upper=end_m), direction="forward") #model_koniec_1 <- step(lm(cena ~ ., data = dane), direction = "forward") # WSTECZNA model_koniec_2 <- step(lm(TwP ~ ., data = dane), direction = "backward") # W OBU KIERUNKACH model_koniec_3 <- step(lm(TwP ~ ., data = dane), direction = "both") model_koniec_1 model_reczny_1 <- lm(TwP ~ tg20cm + tps + tg100cm + tg5cm, dane) model_reczny_2 <- lm(TwP ~ tg20cm + tps + tg50cm + tg5cm, dane) model_reczny_3 <- lm(TwP ~ tg20cm + tps + tg50cm + tg100cm + tg5cm, dane) res_AIC = c(M1_postepujaca = AIC(model_koniec_1), M2_wsteczna = AIC(model_koniec_2), M3_wobukierunkach = AIC(model_koniec_3), m_reczny = AIC(model_reczny_1), m_reczny2 = AIC(model_reczny_2), m_reczny3 = AIC(model_reczny_3), modelml = AIC(model_lm)) which.min(res_AIC) res_AIC summary(model_koniec_1) model_lm m1 = data.frame("Model 1 - forward","TwP ~ tg20cm + tmin + tps + tg100cm + tg5cm", AIC(model_koniec_1), summary(model_koniec_1)$r.squared) m2 = data.frame("Model 2 - backward","TwP ~ tps + tmin + tg20cm + tg100cm", AIC(model_koniec_2), summary(model_koniec_2)$r.squared) m3 = data.frame("Model 3 - both","TwP ~ tps + tmin + tg20cm + tg100cm + tg5cm", AIC(model_koniec_3), summary(model_koniec_3)$r.squared) m4 = data.frame("Model 4 - regresja liniowa", "TwP ~ tps", AIC(model_lm), summary(model_lm)$r.squared) m5 = data.frame("Model 5 - ręczny", "TwP ~ tg5cm + tg10cm + tg50cm + tps + tg100cm", AIC(lm(TwP ~ tg5cm + tg10cm + tg50cm + tps + tg100cm, data = dane)), summary(lm(TwP ~ tg5cm + tg10cm + tg50cm + tps + tg100cm, data = dane))$r.squared) names(m1) = c("Nazwa modelu", "Formuła", "Wartość AIC", "Współczynnik R2") names(m2) = c("Nazwa modelu", "Formuła", "Wartość AIC", "Współczynnik R2") names(m3) = c("Nazwa modelu", "Formuła", "Wartość AIC", "Współczynnik R2") names(m4) = c("Nazwa modelu", "Formuła", "Wartość AIC", "Współczynnik R2") names(m5) = c("Nazwa modelu", "Formuła", "Wartość AIC", "Współczynnik R2") m12 = full_join(m1, m2) m123 = full_join(m12, m3) m1234 = full_join(m123, m4) m12345 = full_join(m1234, m5) View(m12345) write.csv(m12345, "regresja.csv") model_koniec_1$coefficients # współczynnik modelu summary(model_koniec_1) # TwP = (0.19704 * tps + 0.41723 * tg20cm + # 0.05197 * tmin + 0.17560 * tg100cm - 0.11092 * tg5cm) - 1.57185 smr_model_1 <-summary(model_koniec_1) smr_model_1$r.squared # determinacji outlierTest(model_koniec_1) #odstające id 120 dane[c(113,120,140,141), ] png("zad2_a.png") ggplot(dane, aes(x=TwP, y=tps)) + geom_point() + geom_point(data = dane[c(113,120,140,141), ], aes(x=TwP, y=tps), color = "red", size = 4) + xlab("TwP: Temperatura wody w Parsęcie [C]") + ylab("tps: Temperatura powietrza [C]") + geom_smooth() + geom_abline(color = 'grey') + theme_bw() dev.off() png("zad2_b.png") resid_compare(list(model_lm, model_koniec_1)) dev.off()
Editor is loading...