User Tools

Site Tools


sand_box

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
sand_box [2025/09/23 22:47] hkimscilsand_box [2025/12/29 04:46] (current) hkimscil
Line 1: Line 1:
-{{:pasted:20250924-074755.png}} +{{pasted:20251229-044558.png}} 
-{{:pasted:20250924-074713.png}} +<code> 
-{{:pasted:20250923-130207.png}} +library(tidyverse) 
-{{:pasted:20250923-130335.png}} +library(lavaan) 
-{{:pasted:20250923-130603.png}} +cormat = lav_matrix_lower2full(c(1, 
-{{:pasted:20250924-072845.png}} +                                 0.418613, 1, 
-{{:pasted:20250924-074110.png}} +                                 0.4153223, 0.2652651, 1, 
-{{:pasted:20250924-074518.png}}+                                 0.2670484, 0.4315334, 0.2709399, 1)) 
 +colnames(cormat) <rownames(cormat) <- c("X1","X2","X3","Y"
 +cormat 
 + 
 +n_mean = matrix( c(rep(254,4),4.066, 4.024, 4.033, 3.919),  
 +                 ncol=4, nrow = 2, byrow = T ) 
 +n_mean 
 +n_mean <- n_mean %>% 
 +  `colnames<-`(c("X1","X2","X3","Y")) %>%  
 +  `rownames<-`(c("n", "mean")) 
 +n_mean   
 + 
 + 
 + 
 +# Install the MASS package if not already installed 
 +# install.packages("MASS"
 +# Load the package 
 +library(MASS) 
 +library(lavaan) 
 + 
 +#01 데이터 입력 #### 
 +#AC:개인이 성공 성취도,  
 +#IN:가계의 수입,  
 +#AB:개인의 능력,  
 +#SU:개인의성공열정 
 +#covMat  #공분산 행렬로 결과 확인  
 + 
 +# 1. Define the mean vector (for 3 variables) 
 +mns <c(100, 120, 150, 130) 
 +# 2Define the covariance matrix (must be symmetric and positive-definite) 
 +# Example3x3 covariance matrix 
 +cv_mat <lav_matrix_lower2full( 
 +               c(26.5,  
 +                 21.5, 37.9,  
 +                 23.5, 25.2, 41.7, 
 +                 16.5, 14.6, 14.5, 15.8)) 
 +colnames(cvMat) <- rownames(cvMat) <- c("AC", "IN", "AB","SU"
 + 
 +# 3. Simulate the data (e.g., 100 observations) 
 +n_observations <- 300 
 +sim_d <- mvrnorm(n = n_observations, mu = mns, Sigma = cv_mat) 
 +sim_data <- round(sim_d,0) 
 +# View the first few rows of the simulated data 
 +colnames(sim_data) <- c("AC", "IN", "AB","SU"
 +head(sim_data) 
 +str(sim_data) 
 + 
 +PathModel <- ' 
 +SU ~ a*AB +b*IN 
 +AC ~ c*SU + d*AB 
 +AB ~~ IN 
 +
 +#03 분석진행은 sem을 이용하여 한다. #### 
 +# sem(모델, sample.cov= 공분산 행렬, sample.nobs=샘플의 개수) 
 +#fitM <- sem(PathModel, sample.cov = covMat, sample.nobs = 300, 
 +#            fixed.x = FALSE) 
 +fitM <- sem(PathModel, data=sim_data) 
 +#04 결과보기 #### 
 +summary(fitM, standardized=TRUE,fit.measures=TRUE, rsquare=T) 
 + 
 +# install.packages("semPlot"
 +library(semPlot) 
 +semPaths(fitM, whatLabels = "std", curveAdjacent = TRUE,  
 +         style = "lisrel", 
 +         intAtSide=TRUE, intercepts = TRUE, rotation = 2,  
 +         layout = "tree3", 
 +         edge.label.cex = 1,  
 +         edge.label.position=1, edge.color = "black", 
 +         color=list(lat="tomato", man="grey"), 
 +         curvature=3, 
 +         exoVar = TRUE,exoCov = TRUE) 
 + 
 +data(PoliticalDemocracy) 
 +PoliticalDemocracy 
 +sem.model <- " 
 +  # measurement model 
 +    ind60 =~ x1 + x2 + x3 
 +    dem60 =~ y1 + y2 + y3 + y4 
 +    dem65 =~ y5 + y6 + y7 + y8 
 +  # regressions 
 +    dem60 ~ ind60 
 +    dem65 ~ ind60 + dem60 
 +  # residual correlations 
 +    y1 ~~ y5 
 +    y2 ~~ y4 + y6 
 +    y3 ~~ y7 
 +    y4 ~~ y8 
 +    y6 ~~ y8 
 +
 + 
 +# Estimate model  
 +fit1 <- sem(model = sem.model, data = PoliticalDemocracy) 
 + 
 +# show summary result  
 +summary(fit1, fit.measures=TRUE, standardized=TRUE) 
 + 
 +library(lavaan) 
 +library(semPlot) 
 +library(OpenMx) 
 +library(tidyverse) 
 +library(knitr) 
 +library(kableExtra) 
 +library(GGally) 
 + 
 +model <-' 
 +mpg ~ hp + gear + cyl + disp + carb + am + wt 
 +hp ~ cyl + disp + carb 
 +
 +fit.mod <- sem(model, data=mtcars) 
 +summary(fit.mod, standardize=T, rsquare=T) 
 + 
 + 
 +dat <- read.csv("http://commres.net/_media/r/eating.disorder.sim.csv"
 +head(dat) 
 +n <- nrow(dat) 
 +S <- cov(dat) 
 +cor <- cor(dat) 
 + 
 +#  
 +# xSelf-Efficacy = BMI + Self-Esteem + XDisturbance 
 +# Bulimic Symptoms = BMI + Self-Esteem + XSelf-Efficacy + XDisturbance 
 +# Restrictive Symptoms = BMI + Self-Esteem + xSelf-Efficacy + xDisturbance 
 +# Overall Risk = BMI + Self-Esteem + xSelf-Efficacy + Acculturation + xDisturbance 
 +#   
 + 
 +ex1 <- "           #opening a quote 
 +  # Tilda ~ Regression  
 +  # M ~ X regression (X predicts M) 
 +  # Each line corresponds to an equation  
 +  # Disturbance is automatically included for each regression  
 +  # (i.e. no extra term needed) 
 +   
 +  DietSE ~ BMI + SelfEsteem      #DietSE is predicted by BMI and SelfEsteem  
 +  Bulimia ~ DietSE + BMI + SelfEsteem 
 +  Restrictive ~ DietSE + BMI + SelfEsteem 
 +  Risk ~ DietSE + BMI + SelfEsteem + Accu 
 +  
 +ex1.fit <sem(ex1, data=dat) 
 +summary(ex1.fit, standardize=T, rsquare=T) 
 +ex1b.fit <- sem(model = ex1, sample.cov = S, sample.nobs = n) 
 +summary(ex1b.fit) 
 +library(tidyverse) 
 +library(lavaan) 
 +cormat = lav_matrix_lower2full(c(1, 
 +                                 0.418613, 1, 
 +                                 0.4153223, 0.2652651, 1, 
 +                                 0.2670484, 0.4315334, 0.2709399, 1)) 
 +colnames(cormat) <- rownames(cormat) <- c("X1","X2","X3","Y"
 +cormat 
 + 
 +n_mean = matrix( c(rep(254,4),4.066, 4.024, 4.033, 3.919),  
 +                 ncol=4, nrow = 2, byrow = T ) 
 +n_mean 
 +n_mean <- n_mean %>% 
 +  `colnames<-`(c("X1","X2","X3","Y")) %>%  
 +  `rownames<-`(c("n", "mean")) 
 +n_mean   
 + 
 + 
 + 
 +# Install the MASS package if not already installed 
 +# install.packages("MASS"
 +# Load the package 
 +library(MASS) 
 +library(lavaan) 
 + 
 +#01 데이터 입력 #### 
 +#AC:개인이 성공 성취도,  
 +#IN:가계의 수입,  
 +#AB:개인의 능력,  
 +#SU:개인의성공열정 
 +#covMat  #공분산 행렬로 결과 확인  
 + 
 +# 1. Define the mean vector (for 3 variables) 
 +mns <c(100, 120, 150, 130) 
 +# 2Define the covariance matrix (must be symmetric and positive-definite) 
 +# Example3x3 covariance matrix 
 +cv_mat <lav_matrix_lower2full( 
 +               c(26.5,  
 +                 21.5, 37.9,  
 +                 23.5, 25.2, 41.7, 
 +                 16.5, 14.6, 14.5, 15.8)) 
 +colnames(cvMat) <- rownames(cvMat) <- c("AC", "IN", "AB","SU"
 + 
 +# 3. Simulate the data (e.g., 100 observations) 
 +n_observations <- 300 
 +sim_d <- mvrnorm(n = n_observations, mu = mns, Sigma = cv_mat) 
 +sim_data <- round(sim_d,0) 
 +# View the first few rows of the simulated data 
 +colnames(sim_data) <- c("AC", "IN", "AB","SU"
 +head(sim_data) 
 +str(sim_data) 
 + 
 +PathModel <- ' 
 +SU ~ a*AB +b*IN 
 +AC ~ c*SU + d*AB 
 +AB ~~ IN 
 +
 +#03 분석진행은 sem을 이용하여 한다. #### 
 +# sem(모델, sample.cov= 공분산 행렬, sample.nobs=샘플의 개수) 
 +#fitM <- sem(PathModel, sample.cov = covMat, sample.nobs = 300, 
 +#            fixed.x = FALSE) 
 +fitM <- sem(PathModel, data=sim_data) 
 +#04 결과보기 #### 
 +summary(fitM, standardized=TRUE,fit.measures=TRUE, rsquare=T) 
 + 
 +# install.packages("semPlot"
 +library(semPlot) 
 +semPaths(fitM, whatLabels = "std", curveAdjacent = TRUE,  
 +         style = "lisrel", 
 +         intAtSide=TRUE, intercepts = TRUE, rotation = 2,  
 +         layout = "tree3", 
 +         edge.label.cex = 1,  
 +         edge.label.position=1, edge.color = "black", 
 +         color=list(lat="tomato", man="grey"), 
 +         curvature=3, 
 +         exoVar = TRUE,exoCov = TRUE) 
 + 
 +data(PoliticalDemocracy) 
 +PoliticalDemocracy 
 +sem.model <- " 
 +  # measurement model 
 +    ind60 =~ x1 + x2 + x3 
 +    dem60 =~ y1 + y2 + y3 + y4 
 +    dem65 =~ y5 + y6 + y7 + y8 
 +  # regressions 
 +    dem60 ~ ind60 
 +    dem65 ~ ind60 + dem60 
 +  # residual correlations 
 +    y1 ~~ y5 
 +    y2 ~~ y4 + y6 
 +    y3 ~~ y7 
 +    y4 ~~ y8 
 +    y6 ~~ y8 
 +
 + 
 +# Estimate model  
 +fit1 <- sem(model = sem.model, data = PoliticalDemocracy) 
 + 
 +# show summary result  
 +summary(fit1, fit.measures=TRUE, standardized=TRUE) 
 + 
 +library(lavaan) 
 +library(semPlot) 
 +library(OpenMx) 
 +library(tidyverse) 
 +library(knitr) 
 +library(kableExtra) 
 +library(GGally) 
 + 
 +model <-' 
 +mpg ~ hp + gear + cyl + disp + carb + am + wt 
 +hp ~ cyl + disp + carb 
 +
 +fit.mod <- sem(model, data=mtcars) 
 +summary(fit.mod, standardize=T, rsquare=T) 
 + 
 + 
 +dat <- read.csv("http://commres.net/_media/r/eating.disorder.sim.csv"
 +head(dat) 
 +n <- nrow(dat) 
 +S <- cov(dat) 
 +cor <- cor(dat) 
 + 
 +#  
 +# xSelf-Efficacy = BMI + Self-Esteem + XDisturbance 
 +# Bulimic Symptoms = BMI + Self-Esteem + XSelf-Efficacy + XDisturbance 
 +# Restrictive Symptoms = BMI + Self-Esteem + xSelf-Efficacy + xDisturbance 
 +# Overall Risk = BMI + Self-Esteem + xSelf-Efficacy + Acculturation + xDisturbance 
 +#   
 + 
 +ex1 <- "           #opening a quote 
 +  # Tilda ~ Regression  
 +  # M ~ X regression (X predicts M) 
 +  # Each line corresponds to an equation  
 +  # Disturbance is automatically included for each regression  
 +  # (i.e. no extra term needed) 
 +   
 +  DietSE ~ BMI + SelfEsteem      #DietSE is predicted by BMI and SelfEsteem  
 +  Bulimia ~ DietSE + BMI + SelfEsteem 
 +  Restrictive ~ DietSE + BMI + SelfEsteem 
 +  Risk ~ DietSE + BMI + SelfEsteem + Accu 
 +  
 +ex1.fit <sem(ex1, data=dat) 
 +summary(ex1.fit, standardize=T, rsquare=T) 
 +ex1b.fit <- sem(model = ex1, sample.cov = S, sample.nobs = n) 
 +summary(ex1b.fit) 
 + 
 +</code> 
 +<tabbed> 
 +  * sand box:code01 
 +  * *sand box:output01 
 +</tabbed> 
 + 
 + 
 +{{clock}} 
 + 
 + 
 +---- 
 +  graph TD 
 +    A(**mermaid**)-->B((__plugin__)) 
 +    A-->C(((//for//))) 
 +    B-->D[["[[https://www.dokuwiki.org/dokuwiki|Dokuwiki]]"]] 
 +    C-->D 
 \begin{eqnarray*}  \begin{eqnarray*} 
 & & P(A \mid B) = \dfrac{P(A \cap B)}{P(B)}\\ & & P(A \mid B) = \dfrac{P(A \cap B)}{P(B)}\\
Line 22: Line 326:
 & & e^{i \pi} + 1 = 0\;  & & e^{i \pi} + 1 = 0\; 
 \end{eqnarray*} \end{eqnarray*}
- 
- 
-<latex>\setlength{\unitlength}{1mm} \begin{picture}(93,46) \put( 0,14){\vector(1,0){60}} \put(61,13){$x$} \put(20,4){\vector(0,1){37}} \put(19,43){$y$} \put(50,34){\circle*{2}} \put(52,35){$P$} \multiput(20,34)(4,0){8}{\line(1,0){2}} \put(14.5,33.5){$y_P$} \multiput(50,14)(0,4){5}{\line(0,1){2}} \put(48,11){$x_P$} \put( 2,8){\vector(3,1){56}} \put(59,26.5){$x'$} \multiput(50,34)(1.9,-5.7){2} {\line(1,-3){1.2}} \put(52,22){$x_P'$} \multiput(50,34)(-5.8,-1.933){6} {\line(-3,-1){3.6}} \put(12,21){$y_P'$} \put(22,8){\vector(-1,3){10.5}} \put(10,41){$y'$} \end{picture}</latex> 
- 
  
 <WRAP tabs> <WRAP tabs>
Line 33: Line 333:
 </WRAP> </WRAP>
  
-{{tabinclude>page1|Top page,page2|Second page,*page3}}+{{tabinclude>sand_box:page1|Top page,sand_box:page2|Second page,*sand_box:page3}}
 <tabbed> <tabbed>
-  * page1|top page +  * *sand_box:page1 
-  * page2 +  * sand_box:page2 
-  * *page3+  * sand_box:page3
  
 </tabbed> </tabbed>
sand_box.1758667677.txt.gz · Last modified: by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki