library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'ggplot2' was built under R version 4.3.1
## Warning: package 'tibble' was built under R version 4.3.1
## Warning: package 'tidyr' was built under R version 4.3.1
## Warning: package 'readr' was built under R version 4.3.2
## Warning: package 'purrr' was built under R version 4.3.1
## Warning: package 'dplyr' was built under R version 4.3.1
## Warning: package 'stringr' was built under R version 4.3.1
## Warning: package 'forcats' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ltm)
## Warning: package 'ltm' was built under R version 4.3.3
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## 
## The following object is masked from 'package:dplyr':
## 
##     select
## 
## Loading required package: msm
## Warning: package 'msm' was built under R version 4.3.3
## Loading required package: polycor
## Warning: package 'polycor' was built under R version 4.3.3
library(dplyr)
library(stats)
library(fastDummies)
## Warning: package 'fastDummies' was built under R version 4.3.3
## Thank you for using fastDummies!
## To acknowledge our work, please cite the package:
## Kaplan, J. & Schlegel, B. (2023). fastDummies: Fast Creation of Dummy (Binary) Columns and Rows from Categorical Variables. Version 1.7.1. URL: https://github.com/jacobkap/fastDummies, https://jacobkap.github.io/fastDummies/.
df_or <- read.csv2("basak_sayisal_veriler.csv")
df <- df_or[-43,]
#glimpse(df)
extract_factors <- function (df,what,howmany,reduce=0,rotat="promax"){
  cat("________________ START --> ", what, "_____________________") 
  cat("\n")
  center <- function(x) { return (x - mean(x))}
  df_sub <- df %>% dplyr::select(starts_with(what)) %>% mutate(across(everything(), center)) 
  CA <- round(cronbach.alpha(df_sub) $ alpha,2)
  cat("\n")
  cat("cronbach_alpa =", CA) 
  cat("\n")
  if (reduce != 0) df_sub=df_sub[,-reduce]
  FA<- df_sub%>%factanal(.,howmany, scores ="regression",rotation=rotat)
  print(FA $ loadings)
  explained <- 1-FA $ uniquenesses
  barplot(explained,cex.names=0.7, col=1:length(explained),
        main="faktor analizining acikladigi oranlar", cex.main=0.8)
  cat("\n")
  cat("faktor analizining acikladigi oranlar:");cat("\n")
  explained_props <- as.data.frame(1-FA $ uniquenesses)
  colnames(explained_props) ="explained_variances"
  print(explained_props);cat("\n")
  cat("likelihood ratio test | p-value:", FA $ PVAL);  cat("\n")
  if(FA $ PVAL<0.05) print("factors are not sufficient") 
  else cat("\n", "factors are sufficient") 
  cat("\n")
  cat("________________ END _____________________")
 
  outcome <-list(FA,df_sub)
  return(outcome)
}
dummy_func <- function (df,this) {
  dummy <- df %>%
  dplyr::select(starts_with("isletme")) %>% 
  dplyr::select(c(this)) %>% 
  dummy_cols %>%
  dplyr::select(where(is.numeric))
  return(dummy)
}
show_model_details <- function(model_now){
  cat("\n")
  model_now %>% 
  cooks.distance %>% 
  plot(.,type="h",col="black",
       main=paste(model_now $call[2],"cooks distances (verilerin modele etkileri)"), cex.main = 0.6);abline(h=1,lty=2,col="red")
  cat(rep("##",3),sep="")
  paste("Y =", model_now $call[2]) %>% print 
  cat(rep("##",3),sep="")
  cat("\n")
  model_now %>% summary %>% print
  
}
df %>%
  dplyr::select(starts_with("isletme")) %>% names
## [1] "isletme_sektor"         "isletme_isim"           "isletme_yabanci_ortak" 
## [4] "isletme_yas"            "isletme_olcek"          "isletme_calisan_sayisi"
df %>%
  dplyr::select(starts_with("isletme")) %>% 
  dplyr::select(c(3)) %>% table
## isletme_yabanci_ortak
##  Evet Hayir 
##    51    70
df %>%
  dplyr::select(starts_with("isletme")) %>% 
  dplyr::select(c(5,6)) %>% table
##              isletme_calisan_sayisi
## isletme_olcek 10 - 49 kisi 250 kisi ve uzeri 50 - 249 kisi
##         Buyuk            1                36            11
##         Kucuk            9                 0             6
##         Orta             9                 9            40
df %>%
  dplyr::select(starts_with("isletme")) %>% 
  dplyr::select(c(4)) %>% table
## isletme_yas
##      1 - 10 yil     11 - 30 yil 30 yildan fazla 
##              15              51              55
make_model <- function(df,which_X){
dummies<- dummy_func(df,which_X)
df_pilot <- cbind(see_sur_scores,dummies) 
model_sosyal_cevresel_boyut <- lm(df_pilot $sosyal_cevresel_donusum~.,data = df_pilot[,-c(2,3)]) 
model_sosyal_cevresel_boyut%>%show_model_details
model_verimlilik_boyutu <- lm(df_pilot $ verimlilik_boyutu~.,data = df_pilot [,-c(1,3)]) 
model_verimlilik_boyutu%>%show_model_details}
see_sur <- extract_factors(df,"far_sur",2)
## ________________ START -->  far_sur _____________________
## 
## cronbach_alpa = 0.92
## 
## Loadings:
##                        Factor1 Factor2
## far_sur_kaynak                  1.031 
## far_sur_gelecek         0.439   0.459 
## far_sur_adil_is         0.772         
## far_sur_toplum          0.943  -0.143 
## far_sur_cevre_koruma    0.786   0.101 
## far_sur_paydas          0.444   0.378 
## far_sur_eko_performans  0.139   0.533 
## far_sur_calisan_hak     0.715   0.170 
## far_sur_tarim           0.660  -0.105 
## 
##                Factor1 Factor2
## SS loadings      3.467   1.779
## Proportion Var   0.385   0.198
## Cumulative Var   0.385   0.583

## 
## faktor analizining acikladigi oranlar:
##                        explained_variances
## far_sur_kaynak                   0.9435916
## far_sur_gelecek                  0.7231282
## far_sur_adil_is                  0.7080690
## far_sur_toplum                   0.6964625
## far_sur_cevre_koruma             0.7537155
## far_sur_paydas                   0.6064055
## far_sur_eko_performans           0.4214697
## far_sur_calisan_hak              0.7336435
## far_sur_tarim                    0.3367582
## 
## likelihood ratio test | p-value: 0.06571096
## 
##  factors are sufficient
## ________________ END _____________________
see_sur_scores <- see_sur[[1]] $ scores
colnames(see_sur_scores) <- c("sosyal_cevresel_donusum","verimlilik_boyutu")
see_sur_scores %>% boxplot(.,horizontal=TRUE,cex.axis=0.7,
                           col=1:dim(see_sur_scores)[2],
                           main = "Faktor analizinden gelen bagimli degiskenler", cex.main=0.7)

make_model(df,3)
## Warning: Using an external vector in selections was deprecated in tidyselect 1.1.0.
## ℹ Please use `all_of()` or `any_of()` instead.
##   # Was:
##   data %>% select(this)
## 
##   # Now:
##   data %>% select(all_of(this))
## 
## See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

## ######[1] "Y = df_pilot$sosyal_cevresel_donusum ~ ."
## ######
## 
## Call:
## lm(formula = df_pilot$sosyal_cevresel_donusum ~ ., data = df_pilot[, 
##     -c(2, 3)])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.8050 -0.9329  0.4233  0.7697  3.9615 
## 
## Coefficients:
##                             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)                  -0.3573     0.1965  -1.819   0.0715 .
## isletme_yabanci_ortak_Hayir   0.6176     0.2583   2.391   0.0184 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.403 on 119 degrees of freedom
## Multiple R-squared:  0.04584,    Adjusted R-squared:  0.03782 
## F-statistic: 5.717 on 1 and 119 DF,  p-value: 0.01837

## ######[1] "Y = df_pilot$verimlilik_boyutu ~ ."
## ######
## 
## Call:
## lm(formula = df_pilot$verimlilik_boyutu ~ ., data = df_pilot[, 
##     -c(1, 3)])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.1971 -0.4102  0.0624  0.7704  3.4458 
## 
## Coefficients:
##                             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)                   0.2734     0.2022   1.352    0.179  
## isletme_yabanci_ortak_Hayir  -0.4726     0.2659  -1.778    0.078 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.444 on 119 degrees of freedom
## Multiple R-squared:  0.02587,    Adjusted R-squared:  0.01768 
## F-statistic:  3.16 on 1 and 119 DF,  p-value: 0.07803
make_model(df,4)

## ######[1] "Y = df_pilot$sosyal_cevresel_donusum ~ ."
## ######
## 
## Call:
## lm(formula = df_pilot$sosyal_cevresel_donusum ~ ., data = df_pilot[, 
##     -c(2, 3)])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.2879 -1.0077  0.5104  0.8430  4.4785 
## 
## Coefficients:
##                               Estimate Std. Error t value Pr(>|t|)
## (Intercept)                     0.3873     0.3674   1.054    0.294
## `isletme_yas_11 - 30 yil`      -0.6441     0.4180  -1.541    0.126
## `isletme_yas_30 yildan fazla`  -0.2549     0.4145  -0.615    0.540
## 
## Residual standard error: 1.423 on 118 degrees of freedom
## Multiple R-squared:  0.02679,    Adjusted R-squared:  0.01029 
## F-statistic: 1.624 on 2 and 118 DF,  p-value: 0.2015

## ######[1] "Y = df_pilot$verimlilik_boyutu ~ ."
## ######
## 
## Call:
## lm(formula = df_pilot$verimlilik_boyutu ~ ., data = df_pilot[, 
##     -c(1, 3)])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.3766 -0.3086 -0.0749  0.8103  3.0748 
## 
## Coefficients:
##                               Estimate Std. Error t value Pr(>|t|)  
## (Intercept)                    -0.8352     0.3702  -2.256   0.0259 *
## `isletme_yas_11 - 30 yil`       1.0070     0.4211   2.391   0.0184 *
## `isletme_yas_30 yildan fazla`   0.9036     0.4176   2.164   0.0325 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.434 on 118 degrees of freedom
## Multiple R-squared:  0.04799,    Adjusted R-squared:  0.03185 
## F-statistic: 2.974 on 2 and 118 DF,  p-value: 0.05494
make_model(df,5)

## ######[1] "Y = df_pilot$sosyal_cevresel_donusum ~ ."
## ######
## 
## Call:
## lm(formula = df_pilot$sosyal_cevresel_donusum ~ ., data = df_pilot[, 
##     -c(2, 3)])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.4814 -0.9939  0.4672  1.0090  4.2851 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)
## (Intercept)         -0.03352    0.20728  -0.162    0.872
## isletme_olcek_Kucuk  0.38550    0.42480   0.907    0.366
## isletme_olcek_Orta  -0.02977    0.28022  -0.106    0.916
## 
## Residual standard error: 1.436 on 118 degrees of freedom
## Multiple R-squared:  0.008736,   Adjusted R-squared:  -0.008066 
## F-statistic: 0.5199 on 2 and 118 DF,  p-value: 0.5959

## ######[1] "Y = df_pilot$verimlilik_boyutu ~ ."
## ######
## 
## Call:
## lm(formula = df_pilot$verimlilik_boyutu ~ ., data = df_pilot[, 
##     -c(1, 3)])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.0459 -0.4098  0.0221  0.7193  2.9736 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)
## (Intercept)           0.2730     0.2095   1.303    0.195
## isletme_olcek_Kucuk  -0.5322     0.4294  -1.239    0.218
## isletme_olcek_Orta   -0.4319     0.2833  -1.525    0.130
## 
## Residual standard error: 1.452 on 118 degrees of freedom
## Multiple R-squared:  0.02375,    Adjusted R-squared:  0.007199 
## F-statistic: 1.435 on 2 and 118 DF,  p-value: 0.2422
make_model(df,6)

## ######[1] "Y = df_pilot$sosyal_cevresel_donusum ~ ."
## ######
## 
## Call:
## lm(formula = df_pilot$sosyal_cevresel_donusum ~ ., data = df_pilot[, 
##     -c(2, 3)])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5403 -0.9434  0.4964  0.9799  4.2262 
## 
## Coefficients:
##                                            Estimate Std. Error t value Pr(>|t|)
## (Intercept)                                  0.2123     0.3301   0.643    0.521
## `isletme_calisan_sayisi_50 - 249 kisi`      -0.2167     0.3812  -0.568    0.571
## `isletme_calisan_sayisi_250 kisi ve uzeri`  -0.2964     0.3937  -0.753    0.453
## 
## Residual standard error: 1.439 on 118 degrees of freedom
## Multiple R-squared:  0.004788,   Adjusted R-squared:  -0.01208 
## F-statistic: 0.2839 on 2 and 118 DF,  p-value: 0.7534

## ######[1] "Y = df_pilot$verimlilik_boyutu ~ ."
## ######
## 
## Call:
## lm(formula = df_pilot$verimlilik_boyutu ~ ., data = df_pilot[, 
##     -c(1, 3)])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.0807 -0.3374 -0.0095  0.9556  3.0460 
## 
## Coefficients:
##                                            Estimate Std. Error t value Pr(>|t|)
## (Intercept)                                -0.10296    0.33517  -0.307    0.759
## `isletme_calisan_sayisi_50 - 249 kisi`     -0.02114    0.38703  -0.055    0.957
## `isletme_calisan_sayisi_250 kisi ve uzeri`  0.30362    0.39972   0.760    0.449
## 
## Residual standard error: 1.461 on 118 degrees of freedom
## Multiple R-squared:  0.01135,    Adjusted R-squared:  -0.005408 
## F-statistic: 0.6772 on 2 and 118 DF,  p-value: 0.51