Decimal places in Summary(model) output in R - r

I am trying to get more than 2 decimal places from model summary output when I use nnet package. I read other threads regarding this and none of those solutions seem to work for me. I tried:
options(digits=10)
summary(model)
b->h1 i1->h1 i2->h1 i3->h1 i4->h1 i5->h1
0.94 -2.67 0.83 -1.06 -2.51 -0.69
b->o1 h1->o1
1.14 -3.41
b->o2 h1->o2
-0.62 3.92
I also tried:
summary(model,digits=10)
b->h1 i1->h1 i2->h1 i3->h1 i4->h1 i5->h1
0.94 -2.67 0.83 -1.06 -2.51 -0.69
b->o1 h1->o1
1.14 -3.41
b->o2 h1->o2
-0.62 3.92
None of those solutions work for me. I have to use caputure.output after summary output If i output the entire model or use coefnames I can get more than 2 decimal places but that is not going to help me if I use caputre.output.

It's likely that the print method for the object returned by summary is where the two decimal places are coming from. As a first attempt, try
print(summary(model),digits=10) ## or whatever other number of digits
If that doesn't work, try the kind of investigation that was done in this answer:
How to make decimal digits of chisq.test four numbers ?

Just use
summary(model)$wts
This will give you the weights with maximum decimal points.
If you want to have other values, e.g. residuals or so, see the manual, I attach a screenshot of the relevant part:
Just write summary(model) then $ and then e.g. wts to get the weights or e.g. residuals to get the residuals

Related

How do you run a complier average causal effect (CACE) analysis in R?

Context
When conducting a Randomized-Controlled Trial (RCT), some participants are randomly assigned to a treatment condition, and others to a control group. However, not everyone assigned to the treatment might follow the treatment protocol (called "treatment compliance").
According to Sagarin et al. (2014), one sensible approach to address this problem is using the complier average causal effect (CACE), also sometimes known as Local average treatment effect (LATE). According to Wikipedia, it is "the treatment effect for the subset of the sample that takes the treatment if and only if they were assigned to the treatment, otherwise known as the compliers." In other words, it will be useful if a proportion of your participants assigned to the treatment group did not follow the treatment protocol.
Question
How do you run this analysis in R?
I couldn't find anything precise on this from Google and stackoverflow searches.
Also despite my many readings, I still cannot figure out what the expected outcome is supposed to be. When using CACE, what is the outcome? Do you end up with updated scores/data ajusted for treatment non-compliance that you can just plug in your regular analyses (akin to factor scores)? Or do you simply get some kind of number that you have to do something with?
What I've tried
The eefAnalytics package seems to provide the most convenient function for this: caceSRTBoot(). "caceSRTBoot performs exploraty CACE analysis of simple randomised education trials." It allows to specify compliance through a simple compliance percentage (beautifully simple and convenient).
However, I am experiencing some problems installing the eefAnalytics package while trying to test it to see the kind of output it gives:
install.packages("eefAnalytics")
package ‘eefAnalytics’ is not available (for R version 4.0.2)
# Install the latest version of this package by entering the following in R:
install.packages("eefAnalytics", repos="http://R-Forge.R-project.org")
package ‘eefAnalytics’ is not available (for R version 4.0.2)
Warning in install.packages :
unable to access index for repository http://R-Forge.R-project.org/bin/windows/contrib/4.0:
cannot open URL 'http://R-Forge.R-project.org/bin/windows/contrib/4.0/PACKAGES'
Upon closer investigation, Cran says: "Package ‘eefAnalytics’ was removed from the CRAN repository."
The other packages I've looked at (e.g., 1, 2, 3, 4) seemed quite complicated and I couldn't figure them out (they don't have a parameter for % compliance for instance, and I had trouble making their "Run this example" widget work). Is there any other user-friendly package out there? Is there any other way to do this analysis in R? Would anyone have some kind of "tutorial"?
Relevant pages: 1.
Installing the eefAnalytics package
I contacted the eefAnalytics package maintainer through the package documentation. I was told that an updated version will be available soon in R. In the meanwhile, I was able to install the old version of the package from the CRAN archives with:
install.packages("https://cran.r-project.org/src/contrib/Archive/eefAnalytics/eefAnalytics_1.0.6.tar.gz", repos = NULL, type = "source")
Note, however, that I had to manually install packages geoR and metafor separately first (else it was throwing an error).
Running the Causal Average Treatment Effect
Answering the first part of the question:
How do you run this analysis in R?
Running the example available from documentation for a simple randomised trial, we get:
library(eefAnalytics)
data(mstData)
############# weighted ITT ####################################
caceOutput3 <- caceSRTBoot(Posttest~ Prettest+ Intervention,
intervention="Intervention",
compliance = "Percentage_Attendance",nBoot=1000,data=mstData)
cace <- caceOutput3$CACE
cace
Compliance ES LB UB
1 P> 0 0.32 0.04 0.62
2 P> 10 0.32 0.04 0.62
3 P> 20 0.37 0.04 0.72
4 P> 30 0.42 0.05 0.83
5 P> 40 0.47 0.06 0.92
6 P> 50 0.58 0.07 1.18
Complier <- caceOutput3$Compliers
Complier
P > 0 P > 10 P > 20 P > 30 P > 40 P > 50 P > 60 P > 70 P > 80 P > 90
pT 1 1 0.87 0.75 0.69 0.55 0.41 0.31 0.25 0.15
pC 0 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
P=PT-pC 1 1 0.87 0.75 0.69 0.55 0.41 0.31 0.25 0.15
### visualising CACE effect size
plot(caceOutput3)
Interpretation of output
I couldn’t find this information from the package documentation, but from what I understand from the output:
ITT = intent to treat
ES = effect size (Hedge’s g)
LB = lower-bound (of the confidence interval of the effect size)
UB = upper-bound
pT = Percentage of compliers in Treatment group
pC = Percentage of compliers in Control group
P=PT-pC = Percentage of compliers in Treatment group minus Percentage of compliers Control group
P > X = Value for participants with a percentage of compliance greater than X (e.g., 50%)
What is the outcome?
To answer the second part of the question:
When using CACE, what is the outcome?
The main outcome of interest seems to be an adjusted effect size (Hedge's g, which is similar to Cohen's d but better for small sample sizes < 20). It seems only possible to compare two groups together, not more (as Hedge's g, like Cohen's d, can only compare two means at once).
The plot is very useful and allows to see the "improvement" of the effect size as a function of increasing compliance with treatment. What you see in this example is that a higher compliance percentage leads to larger effect sizes, as expected.

Running many multiple OLS regressions at once in R

I want to run time series regressions (Fama-French three factor with my new factor). I have following tables.
Table01
Date Port_01 Port_02 --------- Port_18
01/1965 0.85 0.97 1.86
02/1965 8.96 7.2 0.98
03/1965 8.98 7.9 8.86
Table 02
Date Market SMB HML WXO
01/1965 0.85 0.97 0.86 0.87
02/1965 8.96 7.2 0.98 0.79
03/1965 8.98 7.9 8.86 0.86
I have to run 18 regressions and store their intercepts in a vector.
Something like this
Port_1=inter(1)+Beta1(Market)+Beta2(SMB)+Beta3(HML)+Beta3(WXO)+e
Port_2=inter(2)+Beta1(Market)+Beta2(SMB)+Beta3(HML)+Beta3(WXO)+e
Port_18=inter(18)+Beta1(Market)+Beta2(SMB)+Beta3(HML)+Beta3(WXO)+e
I want these 18 intercepts to be stored in a vector. I can do it individually. But If there is a way to do with coding that will help me a lot of time.
Consider vapply(), a variant of lapply() that allows specification of the output here being atomic numeric vector (length of 1). However, first, you need to merge the tables by Date field and then create a list of Port formulas (assuming that's the needed underlying data). Below runs linear regression, lm, but adjust to actual model which might require adjusting intercept extraction:
data <- merge(Table_01, Table_02, by="Date")
ports <- colnames(Table_01)[2:ncol(Table_01)]
formulas <- paste(ports, "~ Market + SMB + HML + WXO")
intercepts <- vapply(formulas, function(i) {
output <- lm(i, data)
coef(output)["(Intercept)"]
}, numeric(1))

Export summary.PCA (package FactoMineR) into a table, with R-cran

I am currently doing a PCA of some data with 35rows and 21 columńs by using the package FactoMineR of R. I'm doing this for my bachelor thesis and I'm studying forestry, so "I have no clue what I'm doing" :).
It works somehow and the interpretation is another chapter, but my Professors, unfortunately also have no clue what they are doing in this statistics kind of thing, so they expect the results in nice little word-sheets, with the data nicely arranged into tables.
The text-output is printed by me with the following methods:
capture.output(mydata)
summary.PCA(mydata)
summary(mydata)
summary.PCA is a tool directly from the package FactoMineR and I use it because capture.output keeps giving me errors when I try and capture PCA("whatever") with it.
But this output is impossible to import into a table unless I do i all by hand, which I cannot accept as a solution (I very much hope so).
Output like the following.. I don't see a way to put this into a table:
Call:
PCA(mydata)
Eigenvalues
Dim.1 Dim.2 Dim.3 Dim.4 Dim.5 Dim.6 Dim.7 Dim.8 Dim.9 Dim.10 Dim.11 Dim.12 Dim.13 Dim.14 Dim.15 Dim.16 Dim.17 Dim.18 Dim.19 Dim.20 Dim.21
Variance 8.539 2.937 1.896 1.644 1.576 1.071 0.738 0.695 0.652 0.463 0.261 0.184 0.136 0.108 0.049 0.021 0.019 0.010 0.000 0.000 0.000
% of var. 40.662 13.984 9.027 7.830 7.505 5.100 3.517 3.311 3.106 2.203 1.242 0.878 0.650 0.513 0.233 0.102 0.093 0.046 0.000 0.000 0.000
Cumulative % of var. 40.662 54.645 63.672 71.502 79.007 84.107 87.624 90.934 94.041 96.244 97.486 98.363 99.013 99.526 99.759 99.862 99.954 100.000 100.000 100.000 100.000
So is there a way to do this? Do I have to transform the data before I can print it into a table?
I hope very much I have expressed myself clearly!
All the best!
Lukas
The summary.PCA function writes all the tables data are available in the outputs.
So you can do:
res <- PCA(mydata)
res$eig ### and you will have the table with the eigenvalues in an object
res$ind$coord ## and you will have the coordinate of the individuals in an object
write.infile(res,file="OutputFile.csv") ## and all the outputs will be written in a csv file
Hope it helps,
Francois

Extracting output from principal function in psych package as a data frame

When I use the principal function, like in the following code, I get a nice table which gives all the standardized loadings, as well as a table with the eigenvalues and the proportion and cumulative proportion explained.
rotatedpca <- principal(PCFdataset, nfactors = 8, rotate = "varimax", scores = T)
I would like to export this output to an excel file (using WriteXLS), but I can only do that for dataframes, and rotatedpca is not a dataframe and cannot be coerced into one it seems. I am able to extract the standardized loadings by using the following code:
loadings<-as.data.frame(unclass(rotatedpca$loadings))
But I cannot figure out how to access the other information that normally displays when I simply call the principal function, in particular the eigenvalues and the proportion and cumulative variance explained. I tried rotatedcpa$values, but that returns what looks like the eigenvalues for all 12 original variables as factors without rotation, which I don't understand. And I haven't been able to figure out any way to even try to extract the variance explained values. How can I simply create a dataframe that looks like the R output I get below from the principal function, for example?
RC2 RC3 RC8 RC1 RC4 RC5 RC6 RC7
SS loadings 1.52 1.50 1.45 1.44 1.01 1.00 0.99 0.98
Proportion Var 0.13 0.12 0.12 0.12 0.08 0.08 0.08 0.08
Cumulative Var 0.13 0.25 0.37 0.49 0.58 0.66 0.74 0.82
Proportion Explained 0.15 0.15 0.15 0.15 0.10 0.10 0.10 0.10
Cumulative Proportion 0.15 0.31 0.45 0.60 0.70 0.80 0.90 1.00
Thanks for reading my post!
I have just added this feature to the latest (as of today) release of psych 1.3.10.11.
If you either
f3 <- fa(Thurstone,3)
#or
p3 <- principal(Thurstone,3)
#then
p <- print(f3)
p # will give you
p
$Vaccounted
MR1 MR2 MR3
SS loadings 2.6411150 1.8621522 1.4951831
Proportion Var 0.2934572 0.2069058 0.1661315
Cumulative Var 0.2934572 0.5003630 0.6664945
Proportion Explained 0.4402995 0.3104389 0.2492616
Proportion 0.4402995 0.7507384 1.0000000
In general, if you have suggestions or questions re the psych package, you will get a faster answer if you contact me directly.
Bill
Why not this:
capture.output( print(rotatedpca), file="pc.txt")
You can read desired portions into Excel using its Text to Columns... function off the /Data menu. Or you can just paste it into an open blank Excel document and select the rows you want to convert. Use the "fixed" option that will probably be offered automagically.

Statistics Question: Kernel Smoothing in R

I have data of this form:
x y
1 0.19
2 0.26
3 0.40
4 0.58
5 0.59
6 1.24
7 0.68
8 0.60
9 1.12
10 0.80
11 1.20
12 1.17
13 0.39
I'm currently plotting a kernel-smoothed density estimate of the x versus y using this code:
smoothed = ksmooth( d$resi, d$score, bandwidth = 6 )
plot( smoothed )
I simply want a plot of the x versus smoothed(y) values, which is ## Heading ##
However, the documentation for ksmooth suggests that this isn't the best kernel-smoothing package available:
This function is implemented purely
for compatibility with S, although it
is nowhere near as slow as the S
function. Better kernel smoothers are
available in other packages.
What other kernel smoothers are better and where can these smoothers be found?
If you "simply want a plot of the x versus smoothed(y)", then I recommend considering loess in package stats - it's simple, fast and effective. If instead you really want a regression based on kernel smoothing, then you could try locpoly in package KernSmooth or npreg in package np.

Resources