library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(palmerpenguins)
##
## Attaching package: 'palmerpenguins'
## The following objects are masked from 'package:datasets':
##
## penguins, penguins_raw
#1 geom_jitter(),grafiklerde aynı noktaların üst üste binmesini önlemek için kullanılır. Noktaları çok az rastgele kaydırarak gösterir.
data(diamonds)
names(diamonds)
## [1] "carat" "cut" "color" "clarity" "depth" "table" "price"
## [8] "x" "y" "z"
glimpse(diamonds)
## Rows: 53,940
## Columns: 10
## $ carat <dbl> 0.23, 0.21, 0.23, 0.29, 0.31, 0.24, 0.24, 0.26, 0.22, 0.23, 0.…
## $ cut <ord> Ideal, Premium, Good, Premium, Good, Very Good, Very Good, Ver…
## $ color <ord> E, E, E, I, J, J, I, H, E, H, J, J, F, J, E, E, I, J, J, J, I,…
## $ clarity <ord> SI2, SI1, VS1, VS2, SI2, VVS2, VVS1, SI1, VS2, VS1, SI1, VS1, …
## $ depth <dbl> 61.5, 59.8, 56.9, 62.4, 63.3, 62.8, 62.3, 61.9, 65.1, 59.4, 64…
## $ table <dbl> 55, 61, 65, 58, 58, 57, 57, 55, 61, 61, 55, 56, 61, 54, 62, 58…
## $ price <int> 326, 326, 327, 334, 335, 336, 336, 337, 337, 338, 339, 340, 34…
## $ x <dbl> 3.95, 3.89, 4.05, 4.20, 4.34, 3.94, 3.95, 4.07, 3.87, 4.00, 4.…
## $ y <dbl> 3.98, 3.84, 4.07, 4.23, 4.35, 3.96, 3.98, 4.11, 3.78, 4.05, 4.…
## $ z <dbl> 2.43, 2.31, 2.31, 2.63, 2.75, 2.48, 2.47, 2.53, 2.49, 2.39, 2.…
diamonds1 <- diamonds %>%
rename(
karat=carat,
kesim=cut,
renk=color,
berraklik=clarity,
derinlik=depth,
tabla=table,
fiyat=price,
x=x,
y=y,
z=z
)
names(diamonds1)
## [1] "karat" "kesim" "renk" "berraklik" "derinlik" "tabla"
## [7] "fiyat" "x" "y" "z"
diamonds1 <- diamonds1 |>
select(karat, fiyat) |>
na.omit()
ggplot(diamonds1, aes(x=karat, y=fiyat)) +
geom_point() +
labs(x="karat",
y="fiyat(TL)",
title="Karat ile Fiyat İlişkisi")
#pırlantanın karatı arttıkça fiyatı da artıyor. Pozitif korelasyon
var.
peng_mod <- lm(fiyat ~ karat , data = diamonds1)
summary(peng_mod)
##
## Call:
## lm(formula = fiyat ~ karat, data = diamonds1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -18585.3 -804.8 -18.9 537.4 12731.7
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2256.36 13.06 -172.8 <2e-16 ***
## karat 7756.43 14.07 551.4 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1549 on 53938 degrees of freedom
## Multiple R-squared: 0.8493, Adjusted R-squared: 0.8493
## F-statistic: 3.041e+05 on 1 and 53938 DF, p-value: < 2.2e-16
#Intercept e göre fiyat 0 olduğunda karat 3.67 olurdu. #R-squarede baktığımızda,karattaki değişimin %84’ü fiyat ile açıklanıyor. #Residual Standard Error’a baktığımızda model ortalama 0.18 karat hata yapıyor .
coef(peng_mod)
## (Intercept) karat
## -2256.361 7756.426
ggplot(diamonds1, aes(x=karat, y=fiyat)) +
geom_point() +
geom_smooth(method = "lm", se=FALSE, color="purple") +
labs(x= "karat" ,
y= "fiyat(TL)" ,
title= "Basit Doğrusal Regresyon Çizgisi")
## `geom_smooth()` using formula = 'y ~ x'
#Karat arttıkça fiyat da artıyor. Doğrusal ilişki var.
new_peng <- data.frame(karat=10)
predict(peng_mod, newdata = new_peng)
## 1
## 75307.9
#karat eğer 10 olursa fiyat yaklaşık 75307 TL olur.
ggplot(diamonds1, aes(x = karat, y = fiyat)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "red") +
labs(
x = "karat",
y = "fiyat(TL)",
title = "Basit Doğrusal Regresyon"
) +
theme_classic()
## `geom_smooth()` using formula = 'y ~ x'
ggplot(diamonds1, aes(x = karat, y = fiyat)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "red") +
labs(
x = "karat",
y = "fiyat(TL)",
title = "Basit Doğrusal Regresyon"
) +
theme_gray()
## `geom_smooth()` using formula = 'y ~ x'
ggplot(diamonds1, aes(x = karat, y = fiyat)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "red") +
labs(
x = "karat",
y = "fiyat(TL)",
title = "Basit Doğrusal Regresyon"
) +
theme_bw()
## `geom_smooth()` using formula = 'y ~ x'
ggplot(diamonds1, aes(x = karat, y = fiyat)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "red") +
labs(
x = "karat",
y = "fiyat(TL)",
title = "Basit Doğrusal Regresyon"
) +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
ggplot(diamonds1, aes(x = karat, y = fiyat)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "red") +
labs(
x = "karat",
y = "fiyat(TL)",
title = "Basit Doğrusal Regresyon"
) +
theme_light()
## `geom_smooth()` using formula = 'y ~ x'
ggplot(diamonds1, aes(x = karat, y = fiyat)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "red") +
labs(
x = "karat",
y = "fiyat(TL)",
title = "Basit Doğrusal Regresyon"
) +
theme_dark()
## `geom_smooth()` using formula = 'y ~ x'
ggplot(diamonds1, aes(x = karat, y = fiyat)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "red") +
labs(
x = "karat",
y = "fiyat(TL)",
title = "Basit Doğrusal Regresyon"
) +
theme_void()
## `geom_smooth()` using formula = 'y ~ x'