rmatrixregressioncorrelation# R - How do I run a regression based on a correlation matrix rather than raw data?

I would like to run a regression based on a correlation matrix rather than raw data. I have looked at this post, but can't make sense of it. How do I do this in R?

Here is some code:

```
#Correlation matrix.
MyMatrix <- matrix(
c(1.0, 0.1, 0.5, 0.4,
0.1, 1.0, 0.9, 0.3,
0.5, 0.9, 1.0, 0.3,
0.4, 0.3, 0.3, 1.0),
nrow=4,
ncol=4)
df <- as.data.frame(MyMatrix)
colnames(df)[colnames(df)=="V1"] <- "a"
colnames(df)[colnames(df)=="V2"] <- "b"
colnames(df)[colnames(df)=="V3"] <- "c"
colnames(df)[colnames(df)=="V4"] <- "d"
#Assume means and standard deviations as follows:
MEAN.a <- 4.00
MEAN.b <- 3.90
MEAN.c <- 4.10
MEAN.d <- 5.00
SD.a <- 1.01
SD.b <- 0.95
SD.c <- 0.99
SD.d <- 2.20
#Run model [UNSURE ABOUT THIS PART]
library(lavaan)
m1 <- 'd ~ a + b + c'
fit <- sem(m1, ????)
summary(fit, standardize=TRUE)
```

Solution

This should do it. First you can convert your correlation matrix to a covariance matrix:

```
MyMatrix <- matrix(
c(1.0, 0.1, 0.5, 0.4,
0.1, 1.0, 0.9, 0.3,
0.5, 0.9, 1.0, 0.3,
0.4, 0.3, 0.3, 1.0),
nrow=4,
ncol=4)
rownames(MyMatrix) <- colnames(MyMatrix) <- c("a", "b","c","d")
#Assume the following means and standard deviations:
MEAN.a <- 4.00
MEAN.b <- 3.90
MEAN.c <- 4.10
MEAN.d <- 5.00
SD.a <- 1.01
SD.b <- 0.95
SD.c <- 0.99
SD.d <- 2.20
s <- c(SD.a, SD.b, SD.c, SD.d)
m <- c(MEAN.a, MEAN.b, MEAN.c, MEAN.d)
cov.mat <- diag(s) %*% MyMatrix %*% diag(s)
rownames(cov.mat) <- colnames(cov.mat) <- rownames(MyMatrix)
names(m) <- rownames(MyMatrix)
```

Then, you can use `lavaan`

to estimate the model along the lines of the post you mentioned in your question. Note, you need to supply a number of observations to get the sample estimate. I used 100 for the example, but you may want to change it if that doesn't make sense.

```
library(lavaan)
m1 <- 'd ~ a + b + c'
fit <- sem(m1,
sample.cov = cov.mat,
sample.nobs=100,
sample.mean=m,
meanstructure=TRUE)
summary(fit, standardize=TRUE)
# lavaan 0.6-6 ended normally after 44 iterations
#
# Estimator ML
# Optimization method NLMINB
# Number of free parameters 5
#
# Number of observations 100
#
# Model Test User Model:
#
# Test statistic 0.000
# Degrees of freedom 0
#
# Parameter Estimates:
#
# Standard errors Standard
# Information Expected
# Information saturated (h1) model Structured
#
# Regressions:
# Estimate Std.Err z-value P(>|z|) Std.lv Std.all
# d ~
# a 6.317 0.095 66.531 0.000 6.317 2.900
# b 12.737 0.201 63.509 0.000 12.737 5.500
# c -13.556 0.221 -61.307 0.000 -13.556 -6.100
#
# Intercepts:
# Estimate Std.Err z-value P(>|z|) Std.lv Std.all
# .d -14.363 0.282 -50.850 0.000 -14.363 -6.562
#
# Variances:
# Estimate Std.Err z-value P(>|z|) Std.lv Std.all
# .d 0.096 0.014 7.071 0.000 0.096 0.020
#
#
```

- Installing R on Linux: configure: error: libcurl >= 7.28.0 library and headers are required with support for https
- How to do ensembles with time series using AICc?
- planes3d expands and draws the area based on the sphere's radius
- How to extract tag code itself using R, rvest
- How to Display or Print Contents of Environment in R
- How to use Windows user credentials for proxy authentication in R/RStudio
- R reticulate specifying python executable to use
- Replace multiple Instances of a variable name in an R function and save the modified function
- Standardizing address formatting in R
- How to fix "failed to load cairo DLL" in R?
- Using grepl to filter columns names in specific range of columns
- changing the legends in ggplot2 to have groups of similar labels
- How to keep only unique rows but ignore a column?
- convert string date to R Date FAST for all dates
- Add subgroup text to plotly pie chart
- R Shiny : adjust height of DT datatable when fillContainer=TRUE,
- Why do R external pointers' "unusual copying semantics" mean they should not be used stand-alone?
- How to extract somo character after a string with a number of word which can change in R
- What does `se` stand for in geom_smooth(..., se = FALSE)?
- How to find number of rows greater than any values in R
- Align text and reduce space between text and parentheses in plotly hover info box
- Remove outer box of geom_bar plot with broken y-axis
- How to use lag/lead in mutate with an initial value?
- Is it possible to have a Shiny ConditionalPanel whose condition is a global variable?
- counting elements in one list in another list
- How to vectorize nested loops in R?
- Replace NA values with an incrementing sequence starting from the previous non-NA value
- How can I calculate the number of uniques in a row within a species matrix?
- How to perform operations on pairs of rows, based on a "distinguishing" column's values
- Mutate variable based on previous observations