Skip to content

Example 20: Simulation with Varying Sample Size and Parameters

psunthud edited this page Jan 14, 2013 · 2 revisions

Model Description

This example will show how to make the simulation study when the sample size is not equal across replications and the model parameters are random. That is, we will find the sample size value that the power of the target parameter is equal to .8 when the target parameter is actually random. This example is a SEM model with two factors. The first factor contains six indicators. The second factor contains four indicators. The target parameter is the regression coefficient from Factor 1 to Factor 2 that is varied from 0 to 0.9 in standardized scale.

Example 20 Model

Syntax

The SEM model template can be specified:

loading <- matrix(0, 10, 2)
loading[1:6, 1] <- NA
loading[7:10, 2] <- NA
LY <- bind(loading, "runif(1, 0.3, 0.9)")

RPS <- binds(diag(2))

RTE <- binds(diag(10))

path <- matrix(0, 2, 2)
path[2, 1] <- NA
BE <- bind(path, "runif(1, 0, 0.9)")

latentReg <- model(LY = LY, RPS = RPS, RTE = RTE, BE = BE, modelType = "SEM")

The result object can be specified for the varying sample size:

Output <- sim(NULL, n=25:500, latentReg)
summary(Output)

The figure below shows the screen provided by the summary function:

Example 20 summary

Example 20 summary

The cutoffs given the value of sample size can be plotted by the plotCutoff function:

plotCutoff(Output, 0.05)

The figure below shows the graph provided by the plotCutoff function:

Example 20 SSD

The cutoff given the specific value of sample size can be obtained by the getCutoff functions:

getCutoff(Output, 0.05, n = 200)	

The power of each parameter when both sample size and the target parameter (regression coefficient from Factor 1 to Factor 2) are varying can be obtained by the getPower function:

Cpow <- getPower(Output, contParam = "f2~f1")

The figure below shows the first six rows of the Cpow object:

Example 20 Cpow

The contParam argument is used to specify the desired varying parameter, which is "f2~f1" in this example. The power of parameters given a specific value of sample size and the target varying parameter can be obtained:

Cpow2 <- getPower(Output, contParam = "f2~f1", nVal = 200, paramVal = seq(0.1, 0.9, 0.1))

The figure below shows the Cpow2 object:

Example 20 Cpow2

The paramVal argument is used to provide the specific value of the target varying parameter, which is a sequence from 0.1 to 0.9 increased by 0.1 in this example. If the contParam argument has more than one target varying parameter, the paramVal argument must be specified in a list with an appropriate name of the list:

targetVal <- list("f2~f1" = seq(0.1, 0.9, 0.1), "f1=~y1" = c(0.5, 0.7))
Cpow3 <- getPower(Output, contParam = c("f2~f1", "f1=~y1"), nVal = 200, paramVal = targetVal)

The figure below shows the Cpow3 object:

Example 20 Cpow3

The power table obtained from the getPower function can be used to find the sample size value that provides the power of 0.80 given each value of the regression coefficient by the findPower function:

findPower(Cpow, 1, 0.80)

The figure below shows the screen provided by the findPower function for sample size:

Example 20 findpower

Note that 1 is the index of the sample size column. The regression coefficient value that provides the power 0.80 given each value of sample size can be calculated:

findPower(Cpow, 2, 0.80)

The figure below shows the screen of several rows provided by the findPower function for regression coefficients:

Example 20 findpower2

Note that 2 is the index of the "f2~f1" column. The power graphs of the regression coefficient from Factor 1 to Factor 2 and the factor loading from Factor 2 to Indicator 10 against the sample size and the regression coefficient can be built by the plotPower function:

plotPower(Output, powerParam = c("f2~f1", "f2=~y10"), contParam = "f2~f1")

The figure below shows the graph provided by the plotPower function:

Example 20 plotpower

The powerParam argument is the parameter names that we wish to find the power from. The contParam argument is the parameter that we use as the varying parameter in the simulation.

The power can be plotted in a three-dimensional graph by specifying the useContour argument as FALSE:

plotPower(Output, powerParam = c("f2~f1", "f2=~y10"), contParam = "f2~f1", useContour=FALSE)

The figure below shows the graph provided by the plotPower function by specifying useContour as FALSE:

Example 20 plotpower2

Here is the summary of the whole script in this example.

Clone this wiki locally