1 Giriş

Bu dosyada markdown kullanımına ve çeşitli nesnelere ilişkin notlar yer almaktadır.

  • 1’den n’e kadar olan sayıların toplamını hesaplama fonksiyonu yaz.
#toplam <- function() {
#  girdi <- readline("Kaca kadar sayilarin toplami hesaplansin:")
#  cat("Lutfen pozitif bir tam sayi giriniz. \n")
#  n <- as.integer(girdi)
#  sonuc <- sum(1:n)
#  cat("1'den", n,"'e kadar olan sayilarin toplami:",sonuc,"\n")
#}
#toplam(5)

1.1 Grafik Oluşturma ve Büyük Veri Setlerine İlişkin Bazı Bilgileri Edinme

data(WorldPhones) 
data(cars)
data(iris)
dim(cars)
## [1] 50  2
nrow(cars)
## [1] 50
ncol(cars)
## [1] 2
head(cars)
##   speed dist
## 1     4    2
## 2     4   10
## 3     7    4
## 4     7   22
## 5     8   16
## 6     9   10
head(WorldPhones)
##      N.Amer Europe Asia S.Amer Oceania Africa Mid.Amer
## 1951  45939  21574 2876   1815    1646     89      555
## 1956  60423  29990 4708   2568    2366   1411      733
## 1957  64721  32510 5230   2695    2526   1546      773
## 1958  68484  35218 6662   2845    2691   1663      836
## 1959  71799  37598 6856   3000    2868   1769      911
## 1960  76036  40341 8220   3145    3054   1905     1008
tail(iris)
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 145          6.7         3.3          5.7         2.5 virginica
## 146          6.7         3.0          5.2         2.3 virginica
## 147          6.3         2.5          5.0         1.9 virginica
## 148          6.5         3.0          5.2         2.0 virginica
## 149          6.2         3.4          5.4         2.3 virginica
## 150          5.9         3.0          5.1         1.8 virginica
tail(WorldPhones,n=3)
##      N.Amer Europe Asia S.Amer Oceania Africa Mid.Amer
## 1959  71799  37598 6856   3000    2868   1769      911
## 1960  76036  40341 8220   3145    3054   1905     1008
## 1961  79831  43173 9053   3338    3224   2005     1076
example("WorldPhones")
## 
## WrldPh> require(graphics)
## 
## WrldPh> matplot(rownames(WorldPhones), WorldPhones, type = "b", log = "y",
## WrldPh+         xlab = "Year", ylab = "Number of telephones (1000's)")

## 
## WrldPh> legend(1951.5, 80000, colnames(WorldPhones), col = 1:6, lty = 1:5,
## WrldPh+        pch = rep(21, 7))
## 
## WrldPh> title(main = "World phones data: log scale for response")
matplot(rownames(WorldPhones), WorldPhones, type = "b", log = "y",
        xlab = "Year", ylab = "Number of telephones (1000's)")
legend("bottomright", colnames(WorldPhones), col = 1:6, lty = 1:5,
       pch = rep(21, 7))
title(main = "World phones data: log scale for response")

1.2 Veri Setlerinde Bazı İşlemler

data(CTTdata,package = "CTT")
head(CTTdata)
##   i1 i2 i3 i4 i5 i6 i7 i8 i9 i10 i11 i12 i13 i14 i15 i16 i17 i18 i19 i20
## 1  A  B  B  B  B  C  B  C  B   D   D   C   A   B   A   D   B   D   A   C
## 2  C  D  A  D  C  B  D  B  D   A   D   D   A   B   C   C   C   A   D   C
## 3  B  D  C  D  A  B  A  C  B   D   B   A   A   D   D   A   B   C   B   B
## 4  C  C  D  D  D  A  A  D  D   D   A   B   C   B   D   B   C   B   C   A
## 5  A  A  A  D  A  A  D  B  A   C   A   D   C   C   C   C   A   A   A   B
## 6  A  A  B  C  C  A  A  A  A   A   B   C   C   C   C   B   D   C   D   D
#df[,1,drop=FALSE] # bu bicimiyle sutunu da data.frame ceker. kritik
data(diamonds)
subset(diamonds,price > 1000 & cut == c("Fair","Good"))
## # A tibble: 2,672 × 10
##    carat cut   color clarity depth table price     x     y     z
##    <dbl> <ord> <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
##  1  0.7  Good  E     VS2      57.5    58  2759  5.85  5.9   3.38
##  2  0.7  Fair  F     VS2      65.3    55  2762  5.63  5.58  3.66
##  3  0.91 Fair  H     SI2      64.4    57  2763  6.11  6.09  3.93
##  4  0.7  Good  H     VVS2     62.1    64  2767  5.62  5.65  3.5 
##  5  0.71 Good  E     VS2      59.2    61  2772  5.8   5.88  3.46
##  6  0.83 Good  I     VS2      64.6    54  2774  5.85  5.88  3.79
##  7  0.71 Good  F     VS2      63.8    58  2777  5.61  5.64  3.59
##  8  0.7  Good  E     VS2      64.1    59  2777  5.64  5.59  3.6 
##  9  0.98 Fair  H     SI2      67.9    60  2777  6.05  5.97  4.08
## 10  0.7  Good  E     VS1      57.2    62  2782  5.81  5.77  3.31
## # ℹ 2,662 more rows
df2 <- data.frame(
  S1 = sample(0:100,20),
  S2 = runif(n=20,min=50,max=70)
)
df2$S3 <- sample(60:80,20,replace = TRUE)
df2[["ort"]] <- round(rowMeans(df2),2)
head(df2)
##   S1       S2 S3   ort
## 1 76 55.90136 60 63.97
## 2 17 55.45469 63 45.15
## 3 27 54.05751 65 48.69
## 4  4 63.49563 72 46.50
## 5 12 59.80847 74 48.60
## 6 57 59.50933 71 62.50
cbind(df2,S4 = 10)
##    S1       S2 S3   ort S4
## 1  76 55.90136 60 63.97 10
## 2  17 55.45469 63 45.15 10
## 3  27 54.05751 65 48.69 10
## 4   4 63.49563 72 46.50 10
## 5  12 59.80847 74 48.60 10
## 6  57 59.50933 71 62.50 10
## 7  96 69.99334 68 78.00 10
## 8  90 62.61017 64 72.20 10
## 9  42 64.16913 71 59.06 10
## 10 31 53.82784 62 48.94 10
## 11 60 68.61056 62 63.54 10
## 12 46 64.18616 66 58.73 10
## 13 65 64.92957 65 64.98 10
## 14 24 65.62587 72 53.88 10
## 15 52 58.40134 64 58.13 10
## 16 59 52.84055 69 60.28 10
## 17 58 63.31909 75 65.44 10
## 18 55 51.77567 70 58.93 10
## 19 87 65.92127 76 76.31 10
## 20 53 52.87757 70 58.63 10
iris$carpim <- iris$Sepal.Length * iris$Sepal.Width
head(iris[,c(6,1:5)],10)
##    carpim Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1   17.85          5.1         3.5          1.4         0.2  setosa
## 2   14.70          4.9         3.0          1.4         0.2  setosa
## 3   15.04          4.7         3.2          1.3         0.2  setosa
## 4   14.26          4.6         3.1          1.5         0.2  setosa
## 5   18.00          5.0         3.6          1.4         0.2  setosa
## 6   21.06          5.4         3.9          1.7         0.4  setosa
## 7   15.64          4.6         3.4          1.4         0.3  setosa
## 8   17.00          5.0         3.4          1.5         0.2  setosa
## 9   12.76          4.4         2.9          1.4         0.2  setosa
## 10  15.19          4.9         3.1          1.5         0.1  setosa
df2 <- df2[,-4]
df2$S3 <- NULL
head(df2,3)
##   S1       S2
## 1 76 55.90136
## 2 17 55.45469
## 3 27 54.05751
# eklenecek iki satirlik veri seti olusturma
df3 <- data.frame(S1=c(50,60),S2=c(55.3,65.5))
# yeni veri seti
df4 <- rbind (df2,df3)
dim(df4)
## [1] 22  2
str(df4)
## 'data.frame':    22 obs. of  2 variables:
##  $ S1: num  76 17 27 4 12 57 96 90 42 31 ...
##  $ S2: num  55.9 55.5 54.1 63.5 59.8 ...
attributes(df4)
## $names
## [1] "S1" "S2"
## 
## $row.names
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
## 
## $class
## [1] "data.frame"

1.3 Veri Setlerine İlişkin Betimsel İstatistikler Elde Etme

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
summary(cars$speed)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     4.0    12.0    15.0    15.4    19.0    25.0

1.4 Listeler

  • Listeler, bünyesinde çok farklı yapılardaki verileri tutabilir.
###### Listeler
require(stats); require(graphics)
fm1 <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
summary(fm1)
## 
## Call:
## lm(formula = sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.2422 -2.6857 -0.2488  2.4280  9.7509 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 28.5660865  7.3545161   3.884 0.000334 ***
## pop15       -0.4611931  0.1446422  -3.189 0.002603 ** 
## pop75       -1.6914977  1.0835989  -1.561 0.125530    
## dpi         -0.0003369  0.0009311  -0.362 0.719173    
## ddpi         0.4096949  0.1961971   2.088 0.042471 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.803 on 45 degrees of freedom
## Multiple R-squared:  0.3385, Adjusted R-squared:  0.2797 
## F-statistic: 5.756 on 4 and 45 DF,  p-value: 0.0007904
str(fm1)
## List of 12
##  $ coefficients : Named num [1:5] 28.566087 -0.461193 -1.691498 -0.000337 0.409695
##   ..- attr(*, "names")= chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
##  $ residuals    : Named num [1:50] 0.864 0.616 2.219 -0.698 3.553 ...
##   ..- attr(*, "names")= chr [1:50] "Australia" "Austria" "Belgium" "Bolivia" ...
##  $ effects      : Named num [1:50] -68.38 -14.29 7.3 -3.52 -7.94 ...
##   ..- attr(*, "names")= chr [1:50] "(Intercept)" "pop15" "pop75" "dpi" ...
##  $ rank         : int 5
##  $ fitted.values: Named num [1:50] 10.57 11.45 10.95 6.45 9.33 ...
##   ..- attr(*, "names")= chr [1:50] "Australia" "Austria" "Belgium" "Bolivia" ...
##  $ assign       : int [1:5] 0 1 2 3 4
##  $ qr           :List of 5
##   ..$ qr   : num [1:50, 1:5] -7.071 0.141 0.141 0.141 0.141 ...
##   .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..$ : chr [1:50] "Australia" "Austria" "Belgium" "Bolivia" ...
##   .. .. ..$ : chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
##   .. ..- attr(*, "assign")= int [1:5] 0 1 2 3 4
##   ..$ qraux: num [1:5] 1.14 1.17 1.16 1.15 1.05
##   ..$ pivot: int [1:5] 1 2 3 4 5
##   ..$ tol  : num 1e-07
##   ..$ rank : int 5
##   ..- attr(*, "class")= chr "qr"
##  $ df.residual  : int 45
##  $ xlevels      : Named list()
##  $ call         : language lm(formula = sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
##  $ terms        :Classes 'terms', 'formula'  language sr ~ pop15 + pop75 + dpi + ddpi
##   .. ..- attr(*, "variables")= language list(sr, pop15, pop75, dpi, ddpi)
##   .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:5] "sr" "pop15" "pop75" "dpi" ...
##   .. .. .. ..$ : chr [1:4] "pop15" "pop75" "dpi" "ddpi"
##   .. ..- attr(*, "term.labels")= chr [1:4] "pop15" "pop75" "dpi" "ddpi"
##   .. ..- attr(*, "order")= int [1:4] 1 1 1 1
##   .. ..- attr(*, "intercept")= int 1
##   .. ..- attr(*, "response")= int 1
##   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
##   .. ..- attr(*, "predvars")= language list(sr, pop15, pop75, dpi, ddpi)
##   .. ..- attr(*, "dataClasses")= Named chr [1:5] "numeric" "numeric" "numeric" "numeric" ...
##   .. .. ..- attr(*, "names")= chr [1:5] "sr" "pop15" "pop75" "dpi" ...
##  $ model        :'data.frame':   50 obs. of  5 variables:
##   ..$ sr   : num [1:50] 11.43 12.07 13.17 5.75 12.88 ...
##   ..$ pop15: num [1:50] 29.4 23.3 23.8 41.9 42.2 ...
##   ..$ pop75: num [1:50] 2.87 4.41 4.43 1.67 0.83 2.85 1.34 0.67 1.06 1.14 ...
##   ..$ dpi  : num [1:50] 2330 1508 2108 189 728 ...
##   ..$ ddpi : num [1:50] 2.87 3.93 3.82 0.22 4.56 2.43 2.67 6.51 3.08 2.8 ...
##   ..- attr(*, "terms")=Classes 'terms', 'formula'  language sr ~ pop15 + pop75 + dpi + ddpi
##   .. .. ..- attr(*, "variables")= language list(sr, pop15, pop75, dpi, ddpi)
##   .. .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
##   .. .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. .. ..$ : chr [1:5] "sr" "pop15" "pop75" "dpi" ...
##   .. .. .. .. ..$ : chr [1:4] "pop15" "pop75" "dpi" "ddpi"
##   .. .. ..- attr(*, "term.labels")= chr [1:4] "pop15" "pop75" "dpi" "ddpi"
##   .. .. ..- attr(*, "order")= int [1:4] 1 1 1 1
##   .. .. ..- attr(*, "intercept")= int 1
##   .. .. ..- attr(*, "response")= int 1
##   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
##   .. .. ..- attr(*, "predvars")= language list(sr, pop15, pop75, dpi, ddpi)
##   .. .. ..- attr(*, "dataClasses")= Named chr [1:5] "numeric" "numeric" "numeric" "numeric" ...
##   .. .. .. ..- attr(*, "names")= chr [1:5] "sr" "pop15" "pop75" "dpi" ...
##  - attr(*, "class")= chr "lm"
library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
wls <- fa(Harman74.cor$cov,4,fm="wls") 
## Loading required namespace: GPArotation
str(wls)
## List of 46
##  $ residual     : num [1:24, 1:24] 0.4452 -0.0348 -0.0152 0.0401 0.0145 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
##   .. ..$ : chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
##  $ dof          : num 186
##  $ ENull        : num NA
##  $ chi          : num NA
##  $ rms          : num 0.0408
##  $ nh           : logi NA
##  $ EPVAL        : num NA
##  $ crms         : num 0.0497
##  $ EBIC         : num NA
##  $ ESABIC       : num NA
##  $ fit          : num 0.903
##  $ fit.off      : num 0.984
##  $ sd           : num 0.04
##  $ factors      : num 4
##  $ complexity   : Named num [1:24] 1.03 1.04 1.23 1.25 1.05 ...
##   ..- attr(*, "names")= chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
##  $ n.obs        : logi NA
##  $ PVAL         : logi NA
##  $ objective    : num 1.72
##  $ criteria     : Named num [1:3] 1.72 NA NA
##   ..- attr(*, "names")= chr [1:3] "objective" "" ""
##  $ Call         : language fa(r = Harman74.cor$cov, nfactors = 4, fm = "wls")
##  $ null.model   : num 11.4
##  $ null.dof     : num 276
##  $ r.scores     : num [1:4, 1:4] 1 0.484 0.339 0.491 0.484 ...
##  $ R2           : num [1:4] 0.918 0.815 0.859 0.766
##  $ valid        : num [1:4] 0.933 0.862 0.879 0.851
##  $ score.cor    : num [1:4, 1:4] 1 0.627 0.485 0.493 0.627 ...
##  $ weights      : num [1:24, 1:4] -0.02148 -0.00436 0.02699 0.00951 0.16055 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
##   .. ..$ : chr [1:4] "WLS1" "WLS3" "WLS2" "WLS4"
##  $ rotation     : chr "oblimin"
##  $ hyperplane   : Named num [1:4] 14 9 16 14
##   ..- attr(*, "names")= chr [1:4] "WLS1" "WLS3" "WLS2" "WLS4"
##  $ communality  : Named num [1:24] 0.555 0.227 0.344 0.349 0.642 ...
##   ..- attr(*, "names")= chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
##  $ communalities: Named num [1:24] 0.561 0.22 0.356 0.349 0.648 ...
##   ..- attr(*, "names")= chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
##  $ uniquenesses : Named num [1:24] 0.445 0.773 0.656 0.651 0.358 ...
##   ..- attr(*, "names")= chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
##  $ values       : num [1:24] 7.646 1.692 1.221 0.915 0.403 ...
##  $ e.values     : num [1:24] 8.14 2.1 1.69 1.5 1.03 ...
##  $ loadings     : 'loadings' num [1:24, 1:4] 0.0427 0.056 0.0874 0.1782 0.7639 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
##   .. ..$ : chr [1:4] "WLS1" "WLS3" "WLS2" "WLS4"
##  $ model        : num [1:24, 1:24] 0.555 0.353 0.418 0.428 0.306 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
##   .. ..$ : chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
##  $ fm           : chr "wls"
##  $ rot.mat      : num [1:4, 1:4] 0.4887 -0.8531 -0.6178 -0.0472 0.2601 ...
##  $ Phi          : num [1:4, 1:4] 1 0.41 0.295 0.408 0.41 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:4] "WLS1" "WLS3" "WLS2" "WLS4"
##   .. ..$ : chr [1:4] "WLS1" "WLS3" "WLS2" "WLS4"
##  $ Structure    : 'loadings' num [1:24, 1:4] 0.361 0.241 0.29 0.369 0.794 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
##   .. ..$ : chr [1:4] "WLS1" "WLS3" "WLS2" "WLS4"
##  $ method       : chr "regression"
##  $ R2.scores    : Named num [1:4] 0.918 0.815 0.859 0.766
##   ..- attr(*, "names")= chr [1:4] "WLS1" "WLS3" "WLS2" "WLS4"
##  $ r            : num [1:24, 1:24] 1 0.318 0.403 0.468 0.321 0.335 0.304 0.332 0.326 0.116 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
##   .. ..$ : chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
##  $ fn           : chr "fa"
##  $ Vaccounted   : num [1:5, 1:4] 3.996 0.166 0.166 0.348 0.348 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:5] "SS loadings" "Proportion Var" "Cumulative Var" "Proportion Explained" ...
##   .. ..$ : chr [1:4] "WLS1" "WLS3" "WLS2" "WLS4"
##  $ ECV          : Named num [1:4] 0.348 0.592 0.804 1
##   ..- attr(*, "names")= chr [1:4] "WLS1" "WLS3" "WLS2" "WLS4"
##  - attr(*, "class")= chr [1:2] "psych" "fa"
#odev
#Pearson Korelasyon matrisini Markdown'da Latex ile yaz.

1.4.1 Odev

  • Pearson korelasyon formülünü latex ile yazınız. \[r = \frac{\sum (X - \overline{X})(Y - \overline{Y})}{\sqrt{\sum (X - \overline{X})^2 \sum (Y - \overline{Y})^2}}\]