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)
# 2. Define the covariance matrix (must be symmetric and positive-definite)
# Example: 3x3 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)
# 2. Define the covariance matrix (must be symmetric and positive-definite)
# Example: 3x3 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)
- sand_box:code01 - Page not found.
- output01
> rm(list =ls())
> library(readr)
> df <- read.csv("http://commres.net/_media/r/plannedbehavior.csv")
> colnames(df) <- c("att", "nor", "cont", "int", "beh")
> head(df)
att nor cont int beh
1 2.31 2.31 2.03 2.50 2.62
2 4.66 4.01 3.63 3.99 3.64
3 3.85 3.56 4.20 4.35 3.83
4 4.24 2.25 2.84 1.51 2.25
5 2.91 3.31 2.40 1.45 2.00
6 2.99 2.51 2.95 2.59 2.20
>
> attach(df)
>
> lm.ba.01 <- lm(beh ~ att + int)
> lm.ba.02 <- lm(beh ~ int)
> lm.ba.03 <- lm(int ~ att)
> lm.ba.04 <- lm(att ~ int)
> lm.ba.05 <- lm(beh ~ att)
>
> abc <- summary(lm.ba.01)$r.square
> ab <- summary(lm.ba.02)$r.square
> bc <- summary(lm.ba.05)$r.square
> ab+bc
[1] 0.2602023
> abc
[1] 0.1989125
> ab+bc - abc
[1] 0.0612898
>
> int.reg <- lm.ba.03$fitted.values - mean(int)
> # be/abef 부분
> int.res <- summary(lm.ba.03)$residuals
> # af/abef 부분
>
> lm.ba.031 <- lm(beh~int.reg)
> lm.ba.032 <- lm(beh~int.res)
> b2 <- summary(lm.ba.031)$r.square
> a2 <- summary(lm.ba.032)$r.square
> a2
[1] 0.1369399
> b2
[1] 0.06197255
> a2+b2
[1] 0.1989125
> ab
[1] 0.1982297
>
graph TD A(**mermaid**)-->B((__plugin__)) A-->C(((//for//))) B-->D[["[[https://www.dokuwiki.org/dokuwiki|Dokuwiki]]"]] C-->D
\begin{eqnarray*} & & P(A \mid B) = \dfrac{P(A \cap B)}{P(B)}\\ & & P(B \mid A) = \dfrac{P(B \cap A)}{P(A)}\\ \\ & & P(B \vert A) \;\; \text{ vs. } \;\; P(B \mid A) \\ & & P(A \cap B) = P(A \mid B) * P(B) \\ & & P(B \cap A) = P(B \mid A) * P(A) \\ & & P(A \cap B) = P(A, B) \\ \\ & & \frac{3}{4 \pi} \sqrt{4 \cdot x^2 12} \\ & & \lim_{n \to \infty} \sum_{k=1}^n \frac{1}{k^2} = \frac{\pi^2}{6} \\ & & {\it f}(x) = \frac{1}{\sqrt{x} x^2} \\ & & e^{i \pi} + 1 = 0\; \end{eqnarray*}
- Top page
- Second page
- page3
33333
- page1
- page2
- page3
33333
\begin{align*} & \;\;\;\; \sum{(Y_i - \hat{Y_i})^2} \\ &= \sum{(Y_i - (a + bX_i))^2} \;\;\; \because \hat{Y_i} = a + bX_i \\ &= \text{SSE or SS.residual} \;\;\; \text{(and this should be the least value.)} \\ \end{align*}
\begin{align*} &\text{for a (constant)} \\ \\ &\dfrac{\text{d}}{\text{dv}} \sum{(Y_i - (a + bX_i))^2} \\ &= \sum \dfrac{\text{d}}{\text{dv}} {(Y_i - (a + bX_i))^2} \\ &= \sum{2 (Y_i - (a + bX_i))} * (-1) \;\;\;\; \\ &\because \dfrac{\text{d}}{\text{dv for a}} (Y_i - (a+bX_i)) = -1 \\ & = -2 \sum{(Y_i - (a + bX_i))} \\ \\ &\text{in order to have the least value, the above should be zero} \\ \\ &-2 \sum{(Y_i - (a + bX_i))} = 0 \\ &\sum{(Y_i - (a + bX_i))} = 0 \\ &\sum{Y_i} - \sum{a} - b \sum{X_i} = 0 \\ &\sum{Y_i} - n*{a} - b \sum{X_i} = 0 \\ &n*{a} = \sum{Y_i} - b \sum{X_i} \\ &a = \dfrac{\sum{Y_i}}{n} - b \dfrac{\sum{X_i}}{n} \\ &a = \overline{Y} - b \overline{X} \\ \end{align*}
\begin{eqnarray*} \text{for b, (coefficient)} \\ \\ \dfrac{\text{d}}{\text{dv}} \sum{(Y_i - (a + bX_i))^2} & = & \sum \dfrac{\text{d}}{\text{dv}} {(Y_i - (a + bX_i))^2} \\ & = & \sum{2 (Y_i - (a + bX_i))} * (-X_i) \;\;\;\; \\ & \because & \dfrac{\text{d}}{\text{dv for b}} (Y_i - (a+bX_i)) = -X_i \\ & = & -2 \sum{X_i (Y_i - (a + bX_i))} \\ \\ \text{in order to have the least value, the above should be zero} \\ \\ -2 \sum{X_i (Y_i - (a + bX_i))} & = & 0 \\ \sum{X_i (Y_i - (a + bX_i))} & = & 0 \\ \sum{X_i (Y_i - ((\overline{Y} - b \overline{X}) + bX_i))} & = & 0 \\ \sum{X_i ((Y_i - \overline{Y}) - b (X_i - \overline{X})) } & = & 0 \\ \sum{X_i (Y_i - \overline{Y})} - \sum{b X_i (X_i - \overline{X}) } & = & 0 \\ \sum{X_i (Y_i - \overline{Y})} & = & b \sum{X_i (X_i - \overline{X})} \\ b & = & \dfrac{\sum{X_i (Y_i - \overline{Y})}}{\sum{X_i (X_i - \overline{X})}} \\ b & = & \dfrac{\sum{(Y_i - \overline{Y})}}{\sum{(X_i - \overline{X})}} \\ b & = & \dfrac{ \sum{(Y_i - \overline{Y})(X_i - \overline{X})} } {\sum{(X_i - \overline{X})(X_i - \overline{X})}} \\ b & = & \dfrac{ \text{SP} } {\text{SS}_\text{x}} \\ \end{eqnarray*}




