podatki <- read.table("~/domača naloga R/MTRD_domaca_naloga/archive/cardekho_data.csv", header=TRUE, sep=",", dec=".")
head(podatki)
##        Car_Name Year Selling_Price Present_Price Kms_Driven Fuel_Type
## 1          ritz 2014          3.35          5.59      27000    Petrol
## 2           sx4 2013          4.75          9.54      43000    Diesel
## 3          ciaz 2017          7.25          9.85       6900    Petrol
## 4       wagon r 2011          2.85          4.15       5200    Petrol
## 5         swift 2014          4.60          6.87      42450    Diesel
## 6 vitara brezza 2018          9.25          9.83       2071    Diesel
##   Seller_Type Transmission Owner
## 1      Dealer       Manual     0
## 2      Dealer       Manual     0
## 3      Dealer       Manual     0
## 4      Dealer       Manual     0
## 5      Dealer       Manual     0
## 6      Dealer       Manual     0
podatki1 <- podatki[ , c(-1, -8, -7, -9)]
head(podatki1)
##   Year Selling_Price Present_Price Kms_Driven Fuel_Type
## 1 2014          3.35          5.59      27000    Petrol
## 2 2013          4.75          9.54      43000    Diesel
## 3 2017          7.25          9.85       6900    Petrol
## 4 2011          2.85          4.15       5200    Petrol
## 5 2014          4.60          6.87      42450    Diesel
## 6 2018          9.25          9.83       2071    Diesel

opis spremenljivk: Zanima nas, kako je na ceno rabljenih avtov vplivajo začetna cena, prevoženi kilometri, tip goriva in leto nakupa avta.

podatki1$Fuel_TypeF <- factor(podatki1$Fuel_Type,
                             levels= c("Petrol", "Diesel"),
                             labels= c("bencin", "dizel"))
head(podatki1)
##   Year Selling_Price Present_Price Kms_Driven Fuel_Type Fuel_TypeF
## 1 2014          3.35          5.59      27000    Petrol     bencin
## 2 2013          4.75          9.54      43000    Diesel      dizel
## 3 2017          7.25          9.85       6900    Petrol     bencin
## 4 2011          2.85          4.15       5200    Petrol     bencin
## 5 2014          4.60          6.87      42450    Diesel      dizel
## 6 2018          9.25          9.83       2071    Diesel      dizel
summary(podatki1[c("Year", "Selling_Price", "Kms_Driven", "Fuel_TypeF")])
##       Year      Selling_Price      Kms_Driven      Fuel_TypeF 
##  Min.   :2003   Min.   : 0.100   Min.   :   500   bencin:239  
##  1st Qu.:2012   1st Qu.: 0.900   1st Qu.: 15000   dizel : 60  
##  Median :2014   Median : 3.600   Median : 32000   NA's  :  2  
##  Mean   :2014   Mean   : 4.661   Mean   : 36947               
##  3rd Qu.:2016   3rd Qu.: 6.000   3rd Qu.: 48767               
##  Max.   :2018   Max.   :35.000   Max.   :500000
#install.packages("tidyr")

library(tidyr)

podatki2 <- drop_na(podatki1)

summary(podatki2[c("Year", "Selling_Price", "Kms_Driven", "Fuel_TypeF")])
##       Year      Selling_Price      Kms_Driven      Fuel_TypeF 
##  Min.   :2003   Min.   : 0.100   Min.   :   500   bencin:239  
##  1st Qu.:2012   1st Qu.: 0.850   1st Qu.: 15000   dizel : 60  
##  Median :2014   Median : 3.650   Median : 32000               
##  Mean   :2014   Mean   : 4.672   Mean   : 36908               
##  3rd Qu.:2016   3rd Qu.: 6.000   3rd Qu.: 48384               
##  Max.   :2018   Max.   :35.000   Max.   :500000
podatki_stev <- podatki2[ , c("Present_Price", "Year", "Selling_Price", "Kms_Driven", "Fuel_TypeF")]

library(car)
## Loading required package: carData
scatterplotMatrix(podatki_stev,
                  smooth=FALSE)

quantile(podatki2$Present_Price, 0.95)
##    95% 
## 22.785
podatki2$Present_Price_W <- ifelse(test = podatki2$Present_Price>40,
                                   yes = 40,
                                   no = podatki2$Present_Price)
par(mfrow=c(1,2))
hist(podatki2$Present_Price,
     breaks = seq(0, 100, by = 5),
     main = "Histogram Present_Price")

hist(podatki2$Present_Price_W,
     breaks = seq(0, 100, by = 5),
     main = "Histogram Present_Price_W")

fit <- lm(Present_Price_W ~ Year + Selling_Price + Kms_Driven + Fuel_TypeF,
          data = podatki2)

vif(fit)
##          Year Selling_Price    Kms_Driven    Fuel_TypeF 
##      1.512003      1.533110      1.471501      1.491895

Z mulitkolinearnostjo nimamo težav

fit<- lm(Present_Price_W ~ Year + Selling_Price + Kms_Driven + Fuel_TypeF,
         data = podatki2)

podatki2$StdOstanki <- round(rstandard(fit), 3)
podatki2$CooksD <- round(cooks.distance(fit), 3)

hist(podatki2$StdOstanki,
     xlab = "Standardizirani ostanki",
     ylab = "Frekvenca",
     main = "Histogram standardiziranih ostankov")

hist(podatki2$CooksD,
     xlab = "Cookove razdalje",
     ylab = "Frekvenca",
     main = "Histogram Cookovih razdalj")

Iz histograma ni vidnih Cookovih razdalj, imamo pa standardizirane ostanke, ki jih moramo odstraniti.

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
podatki2 <- podatki2 %>% mutate(ID = row_number())
print(podatki2)
##     Year Selling_Price Present_Price Kms_Driven Fuel_Type Fuel_TypeF
## 1   2014          3.35         5.590      27000    Petrol     bencin
## 2   2013          4.75         9.540      43000    Diesel      dizel
## 3   2017          7.25         9.850       6900    Petrol     bencin
## 4   2011          2.85         4.150       5200    Petrol     bencin
## 5   2014          4.60         6.870      42450    Diesel      dizel
## 6   2018          9.25         9.830       2071    Diesel      dizel
## 7   2015          6.75         8.120      18796    Petrol     bencin
## 8   2015          6.50         8.610      33429    Diesel      dizel
## 9   2016          8.75         8.890      20273    Diesel      dizel
## 10  2015          7.45         8.920      42367    Diesel      dizel
## 11  2017          2.85         3.600       2135    Petrol     bencin
## 12  2015          6.85        10.380      51000    Diesel      dizel
## 13  2015          7.50         9.940      15000    Petrol     bencin
## 14  2015          6.10         7.710      26000    Petrol     bencin
## 15  2009          2.25         7.210      77427    Petrol     bencin
## 16  2016          7.75        10.790      43000    Diesel      dizel
## 17  2015          7.25        10.790      41678    Diesel      dizel
## 18  2016          7.75        10.790      43000    Diesel      dizel
## 19  2010          2.65         7.980      41442    Petrol     bencin
## 20  2016          2.85         3.950      25000    Petrol     bencin
## 21  2017          4.90         5.710       2400    Petrol     bencin
## 22  2011          4.40         8.010      50000    Petrol     bencin
## 23  2014          2.50         3.460      45280    Petrol     bencin
## 24  2013          2.90         4.410      56879    Petrol     bencin
## 25  2011          3.00         4.990      20000    Petrol     bencin
## 26  2013          4.15         5.870      55138    Petrol     bencin
## 27  2017          6.00         6.490      16200    Petrol     bencin
## 28  2010          1.95         3.950      44542    Petrol     bencin
## 29  2015          7.45        10.380      45000    Diesel      dizel
## 30  2012          3.10         5.980      51439    Diesel      dizel
## 31  2011          2.35         4.890      54200    Petrol     bencin
## 32  2014          4.95         7.490      39000    Diesel      dizel
## 33  2014          6.00         9.950      45000    Diesel      dizel
## 34  2014          5.50         8.060      45000    Diesel      dizel
## 35  2015          4.65         7.200      48767    Petrol     bencin
## 36  2003          0.35         2.280     127000    Petrol     bencin
## 37  2016          3.00         3.760      10079    Petrol     bencin
## 38  2003          2.25         7.980      62000    Petrol     bencin
## 39  2016          5.85         7.870      24524    Petrol     bencin
## 40  2014          2.55         3.980      46706    Petrol     bencin
## 41  2008          1.95         7.150      58000    Petrol     bencin
## 42  2014          5.50         8.060      45780    Diesel      dizel
## 43  2012          1.25         2.690      50000    Petrol     bencin
## 44  2014          7.50        12.040      15000    Petrol     bencin
## 45  2013          2.65         4.890      64532    Petrol     bencin
## 46  2006          1.05         4.150      65000    Petrol     bencin
## 47  2015          5.80         7.710      25870    Petrol     bencin
## 48  2017          7.75         9.290      37000    Petrol     bencin
## 49  2012         14.90        30.610     104707    Diesel      dizel
## 50  2015         23.00        30.610      40000    Diesel      dizel
## 51  2017         18.00        19.770      15000    Diesel      dizel
## 52  2013         16.00        30.610     135000    Diesel      dizel
## 53  2005          2.75        10.210      90000    Petrol     bencin
## 54  2009          3.60        15.040      70000    Petrol     bencin
## 55  2015          4.50         7.270      40534    Petrol     bencin
## 56  2010          4.75        18.540      50000    Petrol     bencin
## 57  2014          4.10         6.800      39485    Petrol     bencin
## 58  2014         19.99        35.960      41000    Diesel      dizel
## 59  2013          6.95        18.610      40001    Petrol     bencin
## 60  2015          4.50         7.700      40588    Petrol     bencin
## 61  2014         18.75        35.960      78000    Diesel      dizel
## 62  2015         23.50        35.960      47000    Diesel      dizel
## 63  2017         33.00        36.230       6000    Diesel      dizel
## 64  2014          4.75         6.950      45000    Diesel      dizel
## 65  2017         19.75        23.150      11000    Petrol     bencin
## 66  2010          9.25        20.450      59000    Diesel      dizel
## 67  2011          4.35        13.740      88000    Petrol     bencin
## 68  2016         14.25        20.910      12000    Petrol     bencin
## 69  2014          3.95         6.760      71000    Diesel      dizel
## 70  2011          4.50        12.480      45000    Diesel      dizel
## 71  2013          7.45        18.610      56001    Petrol     bencin
## 72  2011          2.65         5.710      43000    Petrol     bencin
## 73  2014          4.90         8.930      83000    Diesel      dizel
## 74  2015          3.95         6.800      36000    Petrol     bencin
## 75  2013          5.50        14.680      72000    Petrol     bencin
## 76  2004          1.50        12.350     135154    Petrol     bencin
## 77  2010          5.25        22.830      80000    Petrol     bencin
## 78  2012         14.50        30.610      89000    Diesel      dizel
## 79  2016         14.73        14.890      23000    Diesel      dizel
## 80  2015          4.75         7.850      40000    Diesel      dizel
## 81  2017         23.00        25.390      15000    Diesel      dizel
## 82  2015         12.50        13.460      38000    Diesel      dizel
## 83  2005          3.49        13.460     197176    Diesel      dizel
## 84  2006          2.50        23.730     142000    Petrol     bencin
## 85  2010         35.00        92.600      78000    Diesel      dizel
## 86  2012          5.90        13.740      56000    Petrol     bencin
## 87  2013          3.45         6.050      47000    Petrol     bencin
## 88  2014          4.75         6.760      40000    Petrol     bencin
## 89  2009          3.80        18.610      62000    Petrol     bencin
## 90  2014         11.25        16.090      58242    Diesel      dizel
## 91  2005          3.51        13.700      75000    Petrol     bencin
## 92  2015         23.00        30.610      40000    Diesel      dizel
## 93  2008          4.00        22.780      89000    Petrol     bencin
## 94  2012          5.85        18.610      72000    Petrol     bencin
## 95  2016         20.75        25.390      29000    Diesel      dizel
## 96  2017         17.00        18.640       8700    Petrol     bencin
## 97  2013          7.05        18.610      45000    Petrol     bencin
## 98  2010          9.65        20.450      50024    Diesel      dizel
## 99  2016          1.75         1.900       3000    Petrol     bencin
## 100 2017          1.70         1.820       1400    Petrol     bencin
## 101 2017          1.65         1.780       4000    Petrol     bencin
## 102 2017          1.45         1.600       1200    Petrol     bencin
## 103 2017          1.35         1.470       4100    Petrol     bencin
## 104 2015          1.35         2.370      21700    Petrol     bencin
## 105 2014          1.35         3.450      16500    Petrol     bencin
## 106 2013          1.25         1.500      15000    Petrol     bencin
## 107 2016          1.20         1.500      18000    Petrol     bencin
## 108 2017          1.20         1.470      11000    Petrol     bencin
## 109 2016          1.20         1.780       6000    Petrol     bencin
## 110 2016          1.15         1.500       8700    Petrol     bencin
## 111 2014          1.15         2.400       7000    Petrol     bencin
## 112 2016          1.15         1.400      35000    Petrol     bencin
## 113 2015          1.15         1.470      17000    Petrol     bencin
## 114 2015          1.11         1.470      17500    Petrol     bencin
## 115 2013          1.10         1.470      33000    Petrol     bencin
## 116 2015          1.10         1.900      14000    Petrol     bencin
## 117 2015          1.10         1.470      26000    Petrol     bencin
## 118 2013          1.05         1.900       5400    Petrol     bencin
## 119 2016          1.05         1.260       5700    Petrol     bencin
## 120 2011          1.05         1.500       6900    Petrol     bencin
## 121 2016          1.05         1.170       6000    Petrol     bencin
## 122 2013          1.00         1.470      46500    Petrol     bencin
## 123 2012          0.95         1.750      11500    Petrol     bencin
## 124 2009          0.90         1.750      40000    Petrol     bencin
## 125 2017          0.90         0.950       1300    Petrol     bencin
## 126 2016          0.75         0.800       7000    Petrol     bencin
## 127 2017          0.80         0.870       3000    Petrol     bencin
## 128 2017          0.78         0.840       5000    Petrol     bencin
## 129 2017          0.75         0.870      11000    Petrol     bencin
## 130 2015          0.75         0.820      18000    Petrol     bencin
## 131 2017          0.75         0.950       3500    Petrol     bencin
## 132 2016          0.72         0.950        500    Petrol     bencin
## 133 2017          0.65         0.810      11800    Petrol     bencin
## 134 2015          0.65         0.740       5000    Petrol     bencin
## 135 2014          0.65         1.200      23500    Petrol     bencin
## 136 2013          0.65         0.787      16000    Petrol     bencin
## 137 2016          0.60         0.870      15000    Petrol     bencin
## 138 2015          0.60         0.950      16600    Petrol     bencin
## 139 2013          0.60         1.200      32000    Petrol     bencin
## 140 2016          0.60         0.800      20000    Petrol     bencin
## 141 2015          0.60         0.840      29000    Petrol     bencin
## 142 2016          0.60         0.840      25000    Petrol     bencin
## 143 2014          0.60         0.990      25000    Petrol     bencin
## 144 2012          0.60         0.810      19000    Petrol     bencin
## 145 2014          0.55         0.787      15000    Petrol     bencin
## 146 2015          0.55         0.840      58000    Petrol     bencin
## 147 2010          0.52         0.940      45000    Petrol     bencin
## 148 2016          0.51         0.940      24000    Petrol     bencin
## 149 2011          0.50         0.826       6000    Petrol     bencin
## 150 2016          0.50         0.550      31000    Petrol     bencin
## 151 2012          0.50         0.990      13000    Petrol     bencin
## 152 2013          0.50         0.990      45000    Petrol     bencin
## 153 2014          0.50         0.880       8000    Petrol     bencin
## 154 2017          0.48         0.510       4300    Petrol     bencin
## 155 2017          0.48         0.520      15000    Petrol     bencin
## 156 2015          0.48         0.840      23000    Petrol     bencin
## 157 2017          0.48         0.540       8600    Petrol     bencin
## 158 2017          0.45         0.510       4000    Petrol     bencin
## 159 2011          0.45         0.950      24000    Petrol     bencin
## 160 2014          0.45         0.826      23000    Petrol     bencin
## 161 2012          0.45         0.990      14500    Petrol     bencin
## 162 2010          0.45         0.950      27000    Petrol     bencin
## 163 2016          0.45         0.540      14000    Petrol     bencin
## 164 2016          0.45         0.540        500    Petrol     bencin
## 165 2016          0.45         0.550       1000    Petrol     bencin
## 166 2014          0.42         0.810      42000    Petrol     bencin
## 167 2013          0.42         0.730      12000    Petrol     bencin
## 168 2015          0.40         0.540      14000    Petrol     bencin
## 169 2012          0.40         0.830       5500    Petrol     bencin
## 170 2015          0.40         0.550       6700    Petrol     bencin
## 171 2014          0.40         0.640      13700    Petrol     bencin
## 172 2017          0.40         0.510       1300    Petrol     bencin
## 173 2015          0.38         0.720      38600    Petrol     bencin
## 174 2011          0.38         0.787      75000    Petrol     bencin
## 175 2011          0.35         1.050      30000    Petrol     bencin
## 176 2016          0.35         0.570      24000    Petrol     bencin
## 177 2014          0.35         0.520      19000    Petrol     bencin
## 178 2010          0.31         1.050     213000    Petrol     bencin
## 179 2012          0.30         0.510      60000    Petrol     bencin
## 180 2016          0.30         0.480      50000    Petrol     bencin
## 181 2013          0.30         0.580      30000    Petrol     bencin
## 182 2013          0.27         0.470      21000    Petrol     bencin
## 183 2008          0.25         0.750      26000    Petrol     bencin
## 184 2008          0.25         0.580       1900    Petrol     bencin
## 185 2010          0.25         0.520      22000    Petrol     bencin
## 186 2013          0.25         0.510      32000    Petrol     bencin
## 187 2013          0.25         0.570      18000    Petrol     bencin
## 188 2005          0.20         0.570      55000    Petrol     bencin
## 189 2008          0.20         0.750      60000    Petrol     bencin
## 190 2012          0.20         0.570      25000    Petrol     bencin
## 191 2007          0.20         0.750      49000    Petrol     bencin
## 192 2013          0.20         0.650      24000    Petrol     bencin
## 193 2008          0.20         0.787      50000    Petrol     bencin
## 194 2015          0.18         0.320      35000    Petrol     bencin
## 195 2008          0.17         0.520     500000    Petrol     bencin
## 196 2010          0.16         0.510      33000    Petrol     bencin
## 197 2011          0.15         0.570      35000    Petrol     bencin
## 198 2007          0.12         0.580      53000    Petrol     bencin
## 199 2006          0.10         0.750      92233    Petrol     bencin
## 200 2010          3.25         6.790      58000    Diesel      dizel
## 201 2015          4.40         5.700      28200    Petrol     bencin
## 202 2011          2.95         4.600      53460    Petrol     bencin
## 203 2015          2.75         4.430      28282    Petrol     bencin
## 204 2016          5.25         5.700       3493    Petrol     bencin
## 205 2017          5.75         7.130      12479    Petrol     bencin
## 206 2015          5.15         5.700      34797    Petrol     bencin
## 207 2017          7.90         8.100       3435    Petrol     bencin
## 208 2015          4.85         5.700      21125    Diesel      dizel
## 209 2012          3.10         4.600      35775    Petrol     bencin
## 210 2015         11.75        14.790      43535    Diesel      dizel
## 211 2016         11.25        13.600      22671    Petrol     bencin
## 212 2011          2.90         6.790      31604    Petrol     bencin
## 213 2017          5.25         5.700      20114    Petrol     bencin
## 214 2012          4.50         9.400      36100    Petrol     bencin
## 215 2016          2.90         4.430      12500    Petrol     bencin
## 216 2016          3.15         4.430      15000    Petrol     bencin
## 217 2014          6.45         9.400      45078    Petrol     bencin
## 218 2012          4.50         9.400      36000    Petrol     bencin
## 219 2017          3.50         4.430      38488    Petrol     bencin
## 220 2013          4.50         6.790      32000    Petrol     bencin
## 221 2014          6.00         7.600      77632    Diesel      dizel
## 222 2015          8.25         9.400      61381    Diesel      dizel
## 223 2013          5.11         9.400      36198    Petrol     bencin
## 224 2011          2.70         4.600      22517    Petrol     bencin
## 225 2015          5.25         5.700      24678    Petrol     bencin
## 226 2011          2.55         4.430      57000    Petrol     bencin
## 227 2012          4.95         9.400      60000    Diesel      dizel
## 228 2012          3.10         6.790      52132    Diesel      dizel
## 229 2013          6.15         9.400      45000    Diesel      dizel
## 230 2017          9.25         9.400      15001    Petrol     bencin
## 231 2015         11.45        14.790      12900    Petrol     bencin
## 232 2013          3.90         5.700      53000    Diesel      dizel
## 233 2015          5.50         5.700       4492    Petrol     bencin
## 234 2017          9.10         9.400      15141    Petrol     bencin
## 235 2016          3.10         4.430      11849    Petrol     bencin
## 236 2015         11.25        13.600      68000    Diesel      dizel
## 237 2013          4.80         9.400      60241    Petrol     bencin
## 238 2012          2.00         4.430      23709    Petrol     bencin
## 239 2012          5.35         9.400      32322    Diesel      dizel
## 240 2015          4.75         7.130      35866    Petrol     bencin
## 241 2014          4.40         7.130      34000    Petrol     bencin
## 242 2016          6.25         7.600       7000    Petrol     bencin
## 243 2013          5.95         9.400      49000    Diesel      dizel
## 244 2012          5.20         9.400      71000    Diesel      dizel
## 245 2012          3.75         6.790      35000    Petrol     bencin
## 246 2015          5.95         9.400      36000    Petrol     bencin
## 247 2013          4.00         4.600      30000    Petrol     bencin
## 248 2016          5.25         7.600      17000    Petrol     bencin
## 249 2016         12.90        13.600      35934    Diesel      dizel
## 250 2013          5.00         9.900      56701    Petrol     bencin
## 251 2015          5.40         6.820      31427    Petrol     bencin
## 252 2014          7.20         9.900      48000    Diesel      dizel
## 253 2013          5.25         9.900      54242    Petrol     bencin
## 254 2012          3.00         5.350      53675    Petrol     bencin
## 255 2016         10.25        13.600      49562    Petrol     bencin
## 256 2015          8.50        13.600      40324    Petrol     bencin
## 257 2015          8.40        13.600      25000    Petrol     bencin
## 258 2014          3.90         7.000      36054    Petrol     bencin
## 259 2016          9.15        13.600      29223    Petrol     bencin
## 260 2016          5.50         5.970       5600    Petrol     bencin
## 261 2015          4.00         5.800      40023    Petrol     bencin
## 262 2016          6.60         7.700      16002    Petrol     bencin
## 263 2015          4.00         7.000      40026    Petrol     bencin
## 264 2017          6.50         8.700      21200    Petrol     bencin
## 265 2014          3.65         7.000      35000    Petrol     bencin
## 266 2016          8.35         9.400      19434    Diesel      dizel
## 267 2017          4.80         5.800      19000    Petrol     bencin
## 268 2015          6.70        10.000      18828    Petrol     bencin
## 269 2011          4.10        10.000      69341    Petrol     bencin
## 270 2009          3.00        10.000      69562    Petrol     bencin
## 271 2015          7.50        10.000      27600    Petrol     bencin
## 272 2010          2.25         7.500      61203    Petrol     bencin
## 273 2014          5.30         6.800      16500    Petrol     bencin
## 274 2016         10.90        13.600      30753    Petrol     bencin
## 275 2015          8.65        13.600      24800    Petrol     bencin
## 276 2015          9.70        13.600      21780    Petrol     bencin
## 277 2016          6.00         8.400       4000    Petrol     bencin
## 278 2014          6.25        13.600      40126    Petrol     bencin
## 279 2015          5.25         5.900      14465    Petrol     bencin
## 280 2006          2.10         7.600      50456    Petrol     bencin
## 281 2014          8.25        14.000      63000    Diesel      dizel
## 282 2016          8.99        11.800       9010    Petrol     bencin
## 283 2013          3.50         5.900       9800    Petrol     bencin
## 284 2016          7.40         8.500      15059    Petrol     bencin
## 285 2016          5.65         7.900      28569    Petrol     bencin
## 286 2015          5.75         7.500      44000    Petrol     bencin
## 287 2015          8.40        13.600      34000    Petrol     bencin
## 288 2016         10.11        13.600      10980    Petrol     bencin
## 289 2014          4.50         6.400      19000    Petrol     bencin
## 290 2015          5.40         6.100      31427    Petrol     bencin
## 291 2016          6.40         8.400      12000    Petrol     bencin
## 292 2010          3.25         9.900      38000    Petrol     bencin
## 293 2014          3.75         6.800      33019    Petrol     bencin
## 294 2015          8.55        13.090      60076    Diesel      dizel
## 295 2016          9.50        11.600      33988    Diesel      dizel
## 296 2015          4.00         5.900      60000    Petrol     bencin
## 297 2009          3.35        11.000      87934    Petrol     bencin
## 298 2017         11.50        12.500       9000    Diesel      dizel
## 299 2016          5.30         5.900       5464    Petrol     bencin
##     Present_Price_W StdOstanki CooksD  ID
## 1             5.590      0.082  0.000   1
## 2             9.540      0.630  0.002   2
## 3             9.850      0.370  0.000   3
## 4             4.150     -0.630  0.001   4
## 5             6.870     -0.066  0.000   5
## 6             9.830     -0.314  0.001   6
## 7             8.120     -0.442  0.000   7
## 8             8.610     -0.137  0.000   8
## 9             8.890     -0.881  0.003   9
## 10            8.920     -0.541  0.001  10
## 11            3.600      0.311  0.000  11
## 12           10.380      0.230  0.000  12
## 13            9.940     -0.138  0.000  13
## 14            7.710     -0.311  0.000  14
## 15            7.210     -0.011  0.000  15
## 16           10.790      0.167  0.000  16
## 17           10.790      0.232  0.000  17
## 18           10.790      0.167  0.000  18
## 19            7.980      0.453  0.000  19
## 20            3.950      0.124  0.000  20
## 21            5.710      0.063  0.000  21
## 22            8.010     -0.252  0.000  22
## 23            3.460     -0.363  0.000  23
## 24            4.410     -0.469  0.000  24
## 25            4.990     -0.483  0.000  25
## 26            5.870     -0.549  0.000  26
## 27            6.490     -0.272  0.000  27
## 28            3.950     -0.663  0.001  28
## 29           10.380     -0.031  0.000  29
## 30            5.980     -0.074  0.000  30
## 31            4.890     -0.388  0.000  31
## 32            7.490      0.004  0.000  32
## 33            9.950      0.339  0.000  33
## 34            8.060     -0.095  0.000  34
## 35            7.200      0.089  0.000  35
## 36            2.280     -2.284  0.055  36
## 37            3.760      0.065  0.000  37
## 38            7.980     -0.791  0.008  38
## 39            7.870      0.062  0.000  39
## 40            3.980     -0.210  0.000  40
## 41            7.150      0.034  0.000  41
## 42            8.060     -0.099  0.000  42
## 43            2.690     -0.427  0.000  43
## 44           12.040      0.426  0.000  44
## 45            4.890     -0.218  0.000  45
## 46            4.150     -1.020  0.006  46
## 47            7.710     -0.164  0.000  47
## 48            9.290     -0.241  0.000  48
## 49           30.610      2.698  0.043  49
## 50           30.610     -0.377  0.001  50
## 51           19.770     -1.305  0.011  51
## 52           30.610      2.191  0.042  52
## 53           10.210      0.001  0.000  53
## 54           15.040      2.172  0.013  54
## 55            7.270      0.233  0.000  55
## 56           18.540      3.156  0.022  56
## 57            6.800      0.079  0.000  57
## 58           35.960      2.859  0.061  58
## 59           18.610      2.715  0.011  59
## 60            7.700      0.386  0.000  60
## 61           35.960      3.261  0.072  61
## 62           35.960      1.286  0.017  62
## 63           36.230     -2.844  0.213  63
## 64            6.950     -0.125  0.000  64
## 65           23.150     -1.030  0.012  65
## 66           20.450      1.691  0.014  66
## 67           13.740      1.613  0.006  67
## 68           20.910      0.694  0.003  68
## 69            6.760      0.056  0.000  69
## 70           12.480      1.426  0.011  70
## 71           18.610      2.386  0.010  71
## 72            5.710     -0.180  0.000  72
## 73            8.930      0.304  0.001  73
## 74            6.800      0.358  0.000  74
## 75           14.680      1.846  0.006  75
## 76           12.350      0.959  0.009  76
## 77           22.830      4.284  0.046  77
## 78           30.610      2.976  0.045  78
## 79           14.890     -1.679  0.013  79
## 80            7.850      0.414  0.001  80
## 81           25.390     -1.760  0.031  81
## 82           13.460     -1.365  0.007  82
## 83           13.460      0.313  0.002  83
## 84           23.730      4.925  0.186  84
## 85           40.000     -4.358  0.715  85
## 86           13.740      1.215  0.002  86
## 87            6.050     -0.098  0.000  87
## 88            6.760     -0.254  0.000  88
## 89           18.610      3.397  0.032  89
## 90           16.090     -0.106  0.000  90
## 91           13.700      0.973  0.007  91
## 92           30.610     -0.377  0.001  92
## 93           22.780      4.468  0.076  93
## 94           18.610      2.893  0.016  94
## 95           25.390     -0.902  0.006  95
## 96           18.640     -1.282  0.014  96
## 97           18.610      2.640  0.011  97
## 98           20.450      1.545  0.013  98
## 99            1.900      0.050  0.000  99
## 100           1.820      0.241  0.000 100
## 101           1.780      0.237  0.000 101
## 102           1.600      0.286  0.000 102
## 103           1.470      0.272  0.000 103
## 104           2.370      0.124  0.000 104
## 105           3.450      0.351  0.000 105
## 106           1.500     -0.474  0.000 106
## 107           1.500      0.093  0.000 107
## 108           1.470      0.308  0.000 108
## 109           1.780      0.259  0.000 109
## 110           1.500      0.169  0.000 110
## 111           2.400      0.126  0.000 111
## 112           1.400     -0.011  0.000 112
## 113           1.470     -0.074  0.000 113
## 114           1.470     -0.057  0.000 114
## 115           1.470     -0.510  0.000 115
## 116           1.900      0.120  0.000 116
## 117           1.470     -0.099  0.000 117
## 118           1.900     -0.181  0.000 118
## 119           1.260      0.148  0.000 119
## 120           1.500     -0.707  0.001 120
## 121           1.170      0.114  0.000 121
## 122           1.470     -0.535  0.000 122
## 123           1.750     -0.406  0.000 123
## 124           1.750     -1.101  0.004 124
## 125           0.950      0.322  0.000 125
## 126           0.800      0.123  0.000 126
## 127           0.870      0.333  0.000 127
## 128           0.840      0.321  0.000 128
## 129           0.870      0.313  0.000 129
## 130           0.820     -0.116  0.000 130
## 131           0.950      0.383  0.000 131
## 132           0.950      0.227  0.000 132
## 133           0.810      0.336  0.000 133
## 134           0.740     -0.025  0.000 134
## 135           1.200     -0.148  0.000 135
## 136           0.787     -0.441  0.000 136
## 137           0.870      0.178  0.000 137
## 138           0.950      0.011  0.000 138
## 139           1.200     -0.357  0.000 139
## 140           0.800      0.125  0.000 140
## 141           0.840     -0.096  0.000 141
## 142           0.840      0.112  0.000 142
## 143           0.990     -0.207  0.000 143
## 144           0.810     -0.612  0.001 144
## 145           0.787     -0.200  0.000 145
## 146           0.840     -0.231  0.000 146
## 147           0.940     -1.043  0.002 147
## 148           0.940      0.197  0.000 148
## 149           0.826     -0.674  0.001 149
## 150           0.550      0.024  0.000 150
## 151           0.990     -0.466  0.000 151
## 152           0.990     -0.454  0.000 152
## 153           0.880     -0.105  0.000 153
## 154           0.510      0.353  0.000 154
## 155           0.520      0.298  0.000 155
## 156           0.840     -0.005  0.000 156
## 157           0.540      0.340  0.000 157
## 158           0.510      0.370  0.000 158
## 159           0.950     -0.703  0.001 159
## 160           0.826     -0.182  0.000 160
## 161           0.990     -0.450  0.000 161
## 162           0.950     -0.907  0.002 162
## 163           0.540      0.138  0.000 163
## 164           0.540      0.213  0.000 164
## 165           0.550      0.213  0.000 165
## 166           0.810     -0.277  0.000 166
## 167           0.730     -0.327  0.000 167
## 168           0.540     -0.024  0.000 168
## 169           0.830     -0.433  0.000 169
## 170           0.550      0.020  0.000 170
## 171           0.640     -0.173  0.000 171
## 172           0.510      0.409  0.000 172
## 173           0.720     -0.085  0.000 173
## 174           0.787     -1.007  0.002 174
## 175           1.050     -0.651  0.001 175
## 176           0.570      0.143  0.000 176
## 177           0.520     -0.220  0.000 177
## 178           1.050     -1.899  0.069 178
## 179           0.510     -0.797  0.001 179
## 180           0.480     -0.007  0.000 180
## 181           0.580     -0.421  0.000 181
## 182           0.470     -0.396  0.000 182
## 183           0.750     -1.256  0.008 183
## 184           0.580     -1.190  0.010 184
## 185           0.520     -0.937  0.003 185
## 186           0.510     -0.432  0.000 186
## 187           0.570     -0.334  0.000 187
## 188           0.570     -2.036  0.033 188
## 189           0.750     -1.415  0.007 189
## 190           0.570     -0.535  0.000 190
## 191           0.750     -1.548  0.012 191
## 192           0.650     -0.314  0.000 192
## 193           0.787     -1.347  0.007 193
## 194           0.320     -0.110  0.000 194
## 195           0.520     -6.187 11.883 195
## 196           0.510     -0.956  0.002 196
## 197           0.570     -0.752  0.001 197
## 198           0.580     -1.591  0.012 198
## 199           0.750     -1.928  0.021 199
## 200           6.790     -0.270  0.000 200
## 201           5.700     -0.211  0.000 201
## 202           4.600     -0.780  0.001 202
## 203           4.430      0.140  0.000 203
## 204           5.700     -0.304  0.000 204
## 205           7.130      0.100  0.000 205
## 206           5.700     -0.613  0.000 206
## 207           8.100     -0.555  0.001 207
## 208           5.700     -0.305  0.000 208
## 209           4.600     -0.570  0.000 209
## 210          14.790     -0.550  0.001 210
## 211          13.600     -0.519  0.001 211
## 212           6.790      0.146  0.000 212
## 213           5.700     -0.209  0.000 213
## 214           9.400      0.458  0.000 214
## 215           4.430      0.340  0.000 215
## 216           4.430      0.204  0.000 216
## 217           9.400     -0.170  0.000 217
## 218           9.400      0.459  0.000 218
## 219           4.430      0.091  0.000 219
## 220           6.790     -0.264  0.000 220
## 221           7.600     -0.687  0.002 221
## 222           9.400     -0.866  0.003 222
## 223           9.400      0.346  0.000 223
## 224           4.600     -0.489  0.000 224
## 225           5.700     -0.606  0.000 225
## 226           4.430     -0.665  0.001 226
## 227           9.400      0.199  0.000 227
## 228           6.790      0.214  0.000 228
## 229           9.400     -0.120  0.000 229
## 230           9.400     -0.814  0.002 230
## 231          14.790     -0.324  0.000 231
## 232           5.700     -0.389  0.001 232
## 233           5.700     -0.618  0.001 233
## 234           9.400     -0.742  0.001 234
## 235           4.430      0.246  0.000 235
## 236          13.600     -0.867  0.003 236
## 237           9.400      0.366  0.000 237
## 238           4.430     -0.028  0.000 238
## 239           9.400      0.155  0.000 239
## 240           7.130      0.086  0.000 240
## 241           7.130      0.081  0.000 241
## 242           7.600     -0.133  0.000 242
## 243           9.400     -0.044  0.000 243
## 244           9.400      0.015  0.000 244
## 245           6.790     -0.101  0.000 245
## 246           9.400      0.310  0.000 246
## 247           4.600     -0.790  0.001 247
## 248           7.600      0.300  0.000 248
## 249          13.600     -1.313  0.007 249
## 250           9.900      0.466  0.000 250
## 251           6.820     -0.317  0.000 251
## 252           9.900     -0.285  0.000 252
## 253           9.900      0.358  0.000 253
## 254           5.350     -0.352  0.000 254
## 255          13.600     -0.177  0.000 255
## 256          13.600      0.543  0.001 256
## 257          13.600      0.675  0.001 257
## 258           7.000      0.267  0.000 258
## 259          13.600      0.473  0.001 259
## 260           5.970     -0.341  0.000 260
## 261           5.800     -0.045  0.000 261
## 262           7.700     -0.318  0.000 262
## 263           7.000      0.383  0.000 263
## 264           8.700      0.246  0.000 264
## 265           7.000      0.395  0.000 265
## 266           9.400     -0.497  0.001 266
## 267           5.800      0.053  0.000 267
## 268          10.000      0.253  0.000 268
## 269          10.000      0.499  0.000 269
## 270          10.000      0.664  0.001 270
## 271          10.000     -0.186  0.000 271
## 272           7.500      0.368  0.000 272
## 273           6.800     -0.380  0.000 273
## 274          13.600     -0.392  0.001 274
## 275          13.600      0.554  0.001 275
## 276          13.600      0.058  0.000 276
## 277           8.400      0.291  0.000 277
## 278          13.600      1.453  0.003 278
## 279           5.900     -0.479  0.000 279
## 280           7.600     -0.213  0.000 280
## 281          14.000      0.589  0.001 281
## 282          11.800      0.018  0.000 282
## 283           5.900      0.027  0.000 283
## 284           8.500     -0.417  0.000 284
## 285           7.900      0.149  0.000 285
## 286           7.500     -0.314  0.000 286
## 287          13.600      0.626  0.001 287
## 288          13.600      0.104  0.000 288
## 289           6.400     -0.146  0.000 289
## 290           6.100     -0.574  0.000 290
## 291           8.400      0.052  0.000 291
## 292           9.900      0.865  0.002 292
## 293           6.800      0.285  0.000 293
## 294          13.090      0.319  0.000 294
## 295          11.600     -0.351  0.000 295
## 296           5.900     -0.119  0.000 296
## 297          11.000      0.750  0.002 297
## 298          12.500     -0.685  0.002 298
## 299           5.900     -0.268  0.000 299
#za lažje nadaljevanje sem dodala stolpec "ID". Sicer bi bilo smiselno, da bi to naredila že na začetku, ampak se mi ni zdelo vredno tvegati in popravljati vse za nazaj.
head(podatki2[order(podatki2$StdOstanki), c("ID", "StdOstanki")], 6)
##      ID StdOstanki
## 195 195     -6.187
## 85   85     -4.358
## 63   63     -2.844
## 36   36     -2.284
## 188 188     -2.036
## 199 199     -1.928
head(podatki2[order(-podatki2$StdOstanki), c("ID", "StdOstanki")], 9)
##    ID StdOstanki
## 84 84      4.925
## 93 93      4.468
## 77 77      4.284
## 89 89      3.397
## 61 61      3.261
## 56 56      3.156
## 78 78      2.976
## 94 94      2.893
## 58 58      2.859
head(podatki2[order(-podatki2$CooksD), c("ID" , "CooksD")], 9)
##      ID CooksD
## 195 195 11.883
## 85   85  0.715
## 63   63  0.213
## 84   84  0.186
## 93   93  0.076
## 61   61  0.072
## 178 178  0.069
## 58   58  0.061
## 36   36  0.055
library(dplyr)
podatkiC <- podatki2 %>%
  filter(!ID %in% c(195, 85, 84, 93, 77, 89, 61, 56))

Odstranili smo osamelce in enote z visokim vplivom (ID 195 je bil oboje)

fit <- lm(Present_Price_W ~ Year + Selling_Price + Kms_Driven + Fuel_TypeF,
          data = podatkiC)

podatkiC$StdOcenVred <- scale(fit$fitted.values)

library(car)
scatterplot(StdOstanki~StdOcenVred,
            ylab = "Standardizirani ostanki",
            xlab = "Standardizirane ocenjene vrednosti",
            smooth = FALSE,
            regLine = FALSE,
            boxplots = FALSE,
            data= podatkiC)

Opažam heteroskedastičnost in nelinearnost.

#install.packages("olsrr")

library(olsrr)
## 
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
## 
##     rivers
ols_test_breusch_pagan(fit)
## 
##  Breusch Pagan Test for Heteroskedasticity
##  -----------------------------------------
##  Ho: the variance is constant            
##  Ha: the variance is not constant        
## 
##                    Data                     
##  -------------------------------------------
##  Response : Present_Price_W 
##  Variables: fitted values of Present_Price_W 
## 
##          Test Summary           
##  -------------------------------
##  DF            =    1 
##  Chi2          =    184.8006 
##  Prob > Chi2   =    4.338395e-42

Predpostavko o homoskedastičnosti zavrnemo.

library(pastecs)
## 
## Attaching package: 'pastecs'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## The following object is masked from 'package:tidyr':
## 
##     extract
round(stat.desc(podatkiC[c("Present_Price_W", "Year", "Selling_Price", "Kms_Driven", "Fuel_TypeF")], basic = FALSE), 2)
##              Present_Price_W    Year Selling_Price   Kms_Driven Fuel_TypeF
## median                  5.98 2015.00          3.51     31000.00         NA
## mean                    7.04 2013.75          4.55     34215.16         NA
## SE.mean                 0.40    0.17          0.28      1604.03         NA
## CI.mean.0.95            0.78    0.33          0.55      3157.01         NA
## var                    45.78    7.97         22.75 748712955.52         NA
## std.dev                 6.77    2.82          4.77     27362.62         NA
## coef.var                0.96    0.00          1.05         0.80         NA
summary(fit)
## 
## Call:
## lm(formula = Present_Price_W ~ Year + Selling_Price + Kms_Driven + 
##     Fuel_TypeF, data = podatkiC)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.6785 -0.9833 -0.0605  0.7924  8.1101 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      5.796e+02  1.194e+02   4.855 1.98e-06 ***
## Year            -2.881e-01  5.925e-02  -4.862 1.92e-06 ***
## Selling_Price    1.364e+00  3.221e-02  42.353  < 2e-16 ***
## Kms_Driven       4.069e-05  6.104e-06   6.666 1.35e-10 ***
## Fuel_TypeFdizel -5.757e-01  3.829e-01  -1.504    0.134    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.088 on 286 degrees of freedom
## Multiple R-squared:  0.906,  Adjusted R-squared:  0.9047 
## F-statistic: 689.5 on 4 and 286 DF,  p-value: < 2.2e-16