Sunday, April 25, 2021

Multigroup Analysis for SEM

 #R 3.6 version is needed for MGA analysis 

#More than two option in a variable Mga is not possible

#install.packages("devtools") 

#library(devtools)

# install "plspm"

#install_github("gastonstat/plspm")

# load plspm

library(plspm)

#require(plspm)

dataset <- read.csv(file.choose())


#make a model

CO=c(0,0,0,0,0)

AT=c(1,0,0,0,0)

SN=c(1,0,0,0,0)

PBC=c(1,0,0,0,0)

PMO=c(0,1,1,1,0)


x=rbind(CO,AT,SN,PBC,PMO)

colnames(x)=rownames(x)

innerplot(x, arr.pos=.6)   #plot the data



out=list(6:9, 10:12, 13:17, 18:21, 22:23)


################

#running modle for total:

xx=plspm(dataset, x, out,

         scheme="path",

         boot.val=T, br=1247)


#Multigroup Analysis using bootstrap 

plspm.groups(xx, dataset$gender,

             method="bootstrap",

             reps=500)

#a

#Multigroup Analysis using bootstrap

plspm.groups(xx, as.factor(gender),

             method="bootstrap",

             reps=500)


#Multigroup Analysis using bootstrap

plspm.groups(xx, dataset$Education.level,

             method="bootstrap",

             reps=500)

#Multigroup Analysis using bootstrap

plspm.groups(xx, dataset$income,

             method="bootstrap",

             reps=500)

#Multigroup Analysis using bootstrap

plspm.groups(xx, dataset$living,

             method="bootstrap",

             reps=500)



Thursday, April 22, 2021

Principal Component Analysis (PCA)

#install.packages("magrittr")  # for piping %>%

#install.packages("ade4")      # PCA computation

#install.packages("factoextra")# PCA visualization

library(psych)

#library(fmsb)

#PCA - principal component analysis

#Covid19 data analysis


mydata<- read.csv(file.choose())

summary(mydata)


#Define_mydataiables

#mydata<- cbind(CO1,CO2,CO3,CO4,CO5,CO6,CO7,CO8,PMO1,PMO2,PMO3,

            #AT1,AT2,AT3,AT4,AT5,AT6,AT7,SN1,SN2,SN3,SN4,

            #PBC1,PBC2,PBC3,PBC4)


#Calculating_Cronbach's Alpha

covid_concern<- alpha(data.frame(mydata[c("CO2", "CO3", "CO5", "CO7")]))

attitude<- alpha(data.frame(mydata[c("SN3", "PBC1", "PBC2")]))

social_norm<- alpha(data.frame(mydata[c("CO4", "AT7", "SN1", "SN2", "PMO1")]))

perc_beh_control<- alpha(data.frame(mydata[c("AT2", "AT4", "AT5", "AT6")]))

perc_mor_obligation<-alpha(data.frame(mydata[c("CO8", "AT1")]))


#KMO

covid_concern<- KMO(data.frame(mydata[c("CO2", "CO3", "CO5", "CO7")]))

attitude<- KMO(data.frame(mydata[c("SN3", "PBC1", "PBC2")]))

social_norm<- KMO(data.frame(mydata[c("CO4", "AT7", "SN1", "SN2", "PMO1")]))

perc_beh_control<- KMO(data.frame(mydata[c("AT2", "AT4", "AT5", "AT6")]))

perc_mor_obligation<-KMO(data.frame(mydata[c("CO8", "AT1")]))


#Bartlett's Test of Sphericity

#$pvalue is the sphericity(sig.) value

covid_concern<-cortest.bartlett(cor(data.frame(mydata[, 1:8])))

attitude<- cortest.bartlett(cor(data.frame(mydata[, 9:15])))

social_norm<- cortest.bartlett(cor(data.frame(mydata[, 16:19])))

perc_beh_control<- cortest.bartlett(cor(data.frame(mydata[, 20:23])))

perc_mor_obligation<-cortest.bartlett(cor(data.frame(mydata[c("CO8", "AT1")])))


#Descriptive statistics

summary(mydata)

cor(mydata)


#Principal Component Analysis

pca1<-princomp(mydata, scores=TRUE, cor=TRUE)

summary(pca1)


#Loading of principal components

loadings(pca1)

#pca1$loadings


#Scree plot of eigenvalues

plot(pca1)

#barplot(pca1)

screeplot(pca1, type="line", main="Scree Plot")



#Biplot of score mydataiables

biplot(pca1)


#Scores of the components

pca1$scores[1:26, ]


#Rotation

#mydataimax(pca1$roatation)

#Promax(pca1$rotation)



#Factor analysis - different results from other softwares and no roation

fa1<- factanal(mydata, factor = 5)

fa1


fa2<- factanal(mydata, factor=5, rotation="varimax",  scores = "regression")

fa2


fa3<- factanal(mydata, factors=5, rotation = "cluster", scores = "regression")

fa3


library(ade4)

library(factoextra)

library(magrittr)

#Visualize eigenvalues (scree plot). Show the percentage of variances explained by each principal component.

fviz_eig(pca1)

#Graph of individuals. Individuals with a similar profile are grouped together.

fviz_pca_ind(pca1,

             col.ind = "cos2", # Color by the quality of representation

             gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),

             repel = TRUE)     # Avoid text overlapping 

#Graph of variables. Positive correlated variables point to the same side of the plot. Negative correlated variables point to opposite sides of the graph.

fviz_pca_var(pca1,

             col.var = "contrib", # Color by contributions to the PC

             gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),

             repel = TRUE     # Avoid text overlapping

)

#Biplot of individuals and variables

fviz_pca_biplot(pca1, repel = TRUE,

                col.var = "#2E9FDF", # Variables color

                col.ind = "#696969"  # Individuals color

)


#The ade4 package creates R base plots.

# Scree plot

screeplot(pca1, main = "Screeplot - Eigenvalues", xlab="Component")


# Correlation circle of variables

s.corcircle(pca1$co)

 





Thursday, April 15, 2021

Descriptive statistics in R

 #Tawkir codehub

data<-read.csv(file.choose())  #to input data

summary(data) #to show all data variables

describe(data) #to show mean and median for numeric data

head(data)  #to show some column and variable names


#Rename one column name

colnames(data)[colnames(data)=="lining"]<- "Living"

#Rename all column names

colnames(data)<- c("Age", "Income", "Gender")

#Rename some column names

colnames(data)[colnames(data) %in% c("CO","PMO")]

               

               #Replace value or character in a column

               data$Gender[data$Gender=="Male"]<- "2"

               data$Gender[data$Gender=="Female"]<- "1"

               ##ingeneral variable convert for all data

               data(data=="NA")<- "0"   #Error handeling

               

               #Percentage and frequenty 

               agef<-table(data$Age)  #frequenty table

               agep<-prop.table(table(data$Age)) #percentage table

               barplot(agep, ylab="%")


table(data$Living)

data$Living[data$Living=="Megacity"]<- "1"

data$Living[data$Living=="City"]<- "2"

data$Living[data$Living=="Town"]<- "2"

data$Living[data$Living=="Village"]<- "2"


table(data$Age)

data$Age[data$Age=="Young"]<-"1"

data$Age[data$Age=="Adult"]<-"2"

data$Age[data$Age=="Old"]<-"3"


table(data$Education)

data$Education[data$Education=="Secondary"]<-"1"

data$Education[data$Education=="Tertiary level (college and university degree)"]<-"2"


table(data$Income)

data$Income[data$Income=="Low"]<-"1"

data$Income[data$Income=="Middle"]<-"2"

data$Income[data$Income=="Higher-middle"]<-"3"

data$Income[data$Income=="Higher"]<-"4"


#convert character to number

data$Living<-as.character(data$Living)

data$Age<-as.character(data$Age)

data$Gender<-as.character(data$Gender)

data$Education<-as.character(data$Education)

data$Income<-as.character(data$Income)


data$Living<-as.numeric(as.character(data$Living))

data$Age<-as.numeric(as.character(data$Age))

data$Gender<-as.numeric(as.character(data$Gender))

data$Education<-as.numeric(as.character(data$Education))

data$Income<-as.numeric(as.character(data$Income))

   

data$Living<-as.factor(data$Living)

data$Age<-as.factor(data$Age)

data$Gender<-as.factor(data$Gender)

data$Education<-as.factor(data$Education)

data$Income<-as.factor(data$Income)


#create new data for trial

female<- ifelse(all$Gender=="Female", 1,0)

male<- ifelse(all$Gender=="Male", 2,0)

young<- ifelse(all$Age=="Young",1,0)



describe(data)

summary(data)

colnames(data)



library(psych)

#Calculating_Cronbach's Alpha

demo<- alpha(data.frame(data[c("Living", "Age", "Gender",

                                       "Education", "Income" )]))

#save the change file for future work

write.csv(data,"E:\\planning''''''''''''''''''''''''\\SEM C19 model sir\\chang variable name main\\CODE\\Covid_main.csv", row.names = FALSE)



Wednesday, April 14, 2021

Reliability test in R

 #Calculating_Cronbach's Alpha

covid_concern<- alpha(data.frame(all[c("CO2", "CO3", "CO5", "CO7")]))

attitude<- alpha(data.frame(all[c("SN3", "PBC1", "PBC2")]))

social_norm<- alpha(data.frame(all[c("CO4", "AT7", "SN1", "SN2", "PMO1")]))

perc_beh_control<- alpha(data.frame(all[c("AT2", "AT4", "AT5", "AT6")]))

perc_mor_obligation<-alpha(data.frame(all[c("CO8", "AT1")]))

Tuesday, April 13, 2021

Conditional statement and new variable creation in R

 # Load data

p <- read.csv(file.choose())


#create new data for trial

#create new data for trial

lw9=ifelse(p$LANE_WIDTH<9, 1,0)

lw10=ifelse(p$LANE_WIDTH>=10 & p$LANE_WIDTH<11, 1,0)

lw11=ifelse(p$LANE_WIDTH>=11 & p$LANE_WIDTH<12, 1,0)

lw12=ifelse(p$LANE_WIDTH>=12 & p$LANE_WIDTH<13, 1,0)

lw13=ifelse(p$LANE_WIDTH>=13 & p$LANE_WIDTH<14, 1,0)

lw14=ifelse(p$LANE_WIDTH>=14 & p$LANE_WIDTH<15, 1,0)

lw15=ifelse(p$LANE_WIDTH>=15 & p$LANE_WIDTH<16, 1,0)

lw16=ifelse(p$LANE_WIDTH>=16 & p$LANE_WIDTH<17, 1,0)

lw17=ifelse(p$LANE_WIDTH>=17 & p$LANE_WIDTH<18, 1,0)

lw18=ifelse(p$LANE_WIDTH>=18 & p$LANE_WIDTH<19, 1,0)

lw19=ifelse(p$LANE_WIDTH>=19 & p$LANE_WIDTH<20, 1,0)

lw20=ifelse(p$LANE_WIDTH>=20 & p$LANE_WIDTH<21, 1,0)

lw21=ifelse(p$LANE_WIDTH>=21 & p$LANE_WIDTH<22, 1,0)

lw22=ifelse(p$LANE_WIDTH>=22, 1,0)


#combined data frame

data=cbind(p, lw9, lw10, lw11, lw12, lw13, lw14, lw15, lw16, lw17, lw18, lw19, lw20, lw21, lw22)


#for letter data

#create new data for trial

female<- ifelse(data$Gender=="Female", 1,0)

male<- ifelse(data$Gender=="Male", 1,0)

describe(newdata$CO2)

#Data to word

#for letter data

#create new data for trial

gender<- ifelse(data$Gender==1, "Female", "Male")