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 <- read.csv2("basak_sayisal_veriler.csv")
#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="explained proportions by factor analysis")
  cat("\n")
  cat("explained_proportions:");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)
}
see_sur <- extract_factors(df,"far_sur",2)
## ________________ START -->  far_sur _____________________
## 
## cronbach_alpa = 0.92
## 
## Loadings:
##                        Factor1 Factor2
## far_sur_kaynak                  1.044 
## far_sur_gelecek         0.542   0.365 
## far_sur_adil_is         0.791         
## far_sur_toplum          0.906  -0.110 
## far_sur_cevre_koruma    0.888         
## far_sur_paydas          0.608   0.216 
## far_sur_eko_performans  0.160   0.506 
## far_sur_calisan_hak     0.808         
## far_sur_tarim           0.678  -0.119 
## 
##                Factor1 Factor2
## SS loadings      4.040   1.562
## Proportion Var   0.449   0.174
## Cumulative Var   0.449   0.622

## 
## explained_proportions:
##                        explained_variances
## far_sur_kaynak                   0.9950000
## far_sur_gelecek                  0.7264461
## far_sur_adil_is                  0.7079855
## far_sur_toplum                   0.6819977
## far_sur_cevre_koruma             0.7664654
## far_sur_paydas                   0.6143465
## far_sur_eko_performans           0.4042705
## far_sur_calisan_hak              0.7504223
## far_sur_tarim                    0.3518575
## 
## likelihood ratio test | p-value: 0.1037971
## 
##  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])

df_sub_2 <- df %>%
  dplyr::select(starts_with("isletmenin")) %>% 
  dplyr::select(c(5)) %>% 
  dummy_cols %>% 
  dplyr::select(where(is.numeric))

df_pilot <- cbind(see_sur_scores,df_sub_2)
df_pilot %>% head(4)
##   sosyal_cevresel_donusum verimlilik_boyutu isletmenin.olcegi._Buyuk
## 1              -1.0300490         1.3127042                        1
## 2              -4.0775289         2.1202934                        0
## 3               0.9681238        -0.1379685                        1
## 4              -0.7814591         1.1287485                        0
##   isletmenin.olcegi._Kucuk isletmenin.olcegi._Orta
## 1                        0                       0
## 2                        0                       1
## 3                        0                       0
## 4                        0                       1
model_sosyal_cevresel_donusum <- lm(df_pilot $sosyal_cevresel_donusum~.,data = df_pilot [,-c(2,3)]) 
cat(rep("__",20),"\n",colnames(df_pilot)[1],"\n")
## __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ 
##  sosyal_cevresel_donusum
model_sosyal_cevresel_donusum%>% summary %>% print
## 
## Call:
## lm(formula = df_pilot$sosyal_cevresel_donusum ~ ., data = df_pilot[, 
##     -c(2, 3)])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.3985 -0.8574  0.4428  0.9172  3.8892 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)
## (Intercept)               0.05096    0.20596   0.247    0.805
## isletmenin.olcegi._Kucuk -0.13362    0.41192  -0.324    0.746
## isletmenin.olcegi._Orta  -0.07033    0.27844  -0.253    0.801
## 
## Residual standard error: 1.427 on 119 degrees of freedom
## Multiple R-squared:  0.001054,   Adjusted R-squared:  -0.01573 
## F-statistic: 0.0628 on 2 and 119 DF,  p-value: 0.9392
model_sosyal_verimlilik_boyutu<-lm(df_pilot $ verimlilik_boyutu~.,data = df_pilot [,-c(1,3)]) 
cat(rep("__",20),"\n",colnames(df_pilot)[2],"\n")
## __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ 
##  verimlilik_boyutu
model_sosyal_verimlilik_boyutu%>% summary %>% print
## 
## Call:
## lm(formula = df_pilot$verimlilik_boyutu ~ ., data = df_pilot[, 
##     -c(1, 3)])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.8541 -0.3437  0.0588  0.8463  5.1661 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)
## (Intercept)                0.2057     0.2112   0.974    0.332
## isletmenin.olcegi._Kucuk  -0.1096     0.4223  -0.259    0.796
## isletmenin.olcegi._Orta   -0.4024     0.2855  -1.410    0.161
## 
## Residual standard error: 1.463 on 119 degrees of freedom
## Multiple R-squared:  0.01707,    Adjusted R-squared:  0.0005541 
## F-statistic: 1.034 on 2 and 119 DF,  p-value: 0.3589
see_eko<- extract_factors(df,"far_eko",2)
## ________________ START -->  far_eko _____________________
## 
## cronbach_alpa = 0.82
## 
## Loadings:
##                    Factor1 Factor2
## far_eko_verimlilik  0.851         
## far_eko_satin_alma  0.787   0.104 
## far_eko_teknoloji   0.861  -0.126 
## far_eko_maliyet             1.002 
## far_eko_karlilik            0.862 
## 
##                Factor1 Factor2
## SS loadings      2.087   1.775
## Proportion Var   0.417   0.355
## Cumulative Var   0.417   0.772

## 
## explained_proportions:
##                    explained_variances
## far_eko_verimlilik           0.7471410
## far_eko_satin_alma           0.6940722
## far_eko_teknoloji            0.6737947
## far_eko_maliyet              0.9950000
## far_eko_karlilik             0.7549521
## 
## likelihood ratio test | p-value: 0.5832917
## 
##  factors are sufficient
## ________________ END _____________________
see_eko_scores <- see_eko[[1]] $ scores
colnames(see_eko_scores) <- c("verim_satin","tek_kar_maliyet")
see_eko_scores %>% boxplot(.,horizontal=TRUE,cex.axis=0.7,col=1:dim(see_eko_scores)[2])

df_sub_2 <- df %>%
  dplyr::select(starts_with("isletmenin")) %>% 
  dplyr::select(c(5)) %>% 
  dummy_cols %>% 
  dplyr::select(where(is.numeric))

df_pilot <- cbind(see_eko_scores,df_sub_2) 
#  filter(verim_satin > -2 & tek_kar_maliyet >-3)
#df_pilot[,1:2] %>% boxplot(.,horizontal=TRUE, cex.axis=0.7,col=1:dim(see_eko_scores)[2])

df_pilot %>% head(4)
##   verim_satin tek_kar_maliyet isletmenin.olcegi._Buyuk isletmenin.olcegi._Kucuk
## 1  -0.2049273       1.3420314                        1                        0
## 2  -1.7484247       0.8438739                        0                        0
## 3   1.3329562      -0.3392091                        1                        0
## 4  -0.3420535       0.2909294                        0                        0
##   isletmenin.olcegi._Orta
## 1                       0
## 2                       1
## 3                       0
## 4                       1
model_sosyal_verim_satin <- lm(df_pilot$verim_satin~.,data = df_pilot[,-c(2,3)]) 
cat(rep("__",20),"\n",colnames(df_pilot)[1],"\n")
## __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ 
##  verim_satin
model_sosyal_cevresel_donusum %>% summary %>% print
## 
## Call:
## lm(formula = df_pilot$sosyal_cevresel_donusum ~ ., data = df_pilot[, 
##     -c(2, 3)])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.3985 -0.8574  0.4428  0.9172  3.8892 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)
## (Intercept)               0.05096    0.20596   0.247    0.805
## isletmenin.olcegi._Kucuk -0.13362    0.41192  -0.324    0.746
## isletmenin.olcegi._Orta  -0.07033    0.27844  -0.253    0.801
## 
## Residual standard error: 1.427 on 119 degrees of freedom
## Multiple R-squared:  0.001054,   Adjusted R-squared:  -0.01573 
## F-statistic: 0.0628 on 2 and 119 DF,  p-value: 0.9392
model_sosyal_tek_kar_maliyet<-lm(df_pilot $ tek_kar_maliyet~.,data = df_pilot[,-c(1,3)]) 
cat(rep("__",20),"\n",colnames(df_pilot)[2],"\n")
## __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ 
##  tek_kar_maliyet
model_sosyal_verimlilik_boyutu%>% summary %>% print
## 
## Call:
## lm(formula = df_pilot$verimlilik_boyutu ~ ., data = df_pilot[, 
##     -c(1, 3)])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.8541 -0.3437  0.0588  0.8463  5.1661 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)
## (Intercept)                0.2057     0.2112   0.974    0.332
## isletmenin.olcegi._Kucuk  -0.1096     0.4223  -0.259    0.796
## isletmenin.olcegi._Orta   -0.4024     0.2855  -1.410    0.161
## 
## Residual standard error: 1.463 on 119 degrees of freedom
## Multiple R-squared:  0.01707,    Adjusted R-squared:  0.0005541 
## F-statistic: 1.034 on 2 and 119 DF,  p-value: 0.3589