LOWESS does not provide p-value or confidence intervals, therefore we cannot make any inference with the non-linear curve from LOWESS. R software allows you to fit non-linear curve with p-value and confidence intervals. R-tutorial to fit non-linear curve with 2.18.funding.sav data. Open R (download R from http://www.r-project.org if have not done so) Go to Packages, and then select Load packages, load Base and Foreign package We are going to use Dr. Harrell’s libraries, first load Hmisc and Design libraries from MENU Go to Packages, and then select Install packages from CRAN Load Hmisc and Design Delete downloaded files (y/N)? N 2.4 19.4 R-tutorial to fit non-linear curve with 2.18.funding.sav data (1)
21
Embed
LOWESS does not provide p-value or confidence intervals, therefore we cannot make any inference with the non-linear curve from LOWESS. R software allows.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
LOWESS does not provide p-value or confidence intervals, therefore we cannot make any inference with the non-linear curve from LOWESS. R software allows you to fit non-linear curve with p-value and confidence intervals.
R-tutorial to fit non-linear curve with 2.18.funding.sav data.
Open R (download R from http://www.r-project.org if have not done so)
Go to Packages, and then select Load packages, load Base and Foreign package
We are going to use Dr. Harrell’s libraries, first load Hmisc and Design libraries from MENU
Go to Packages, and then select Install packages from CRAN
Load Hmisc and Design
Delete downloaded files (y/N)? N
#R is capital letter sensitive
2.419.4 R-tutorial to fit non-linear curve with 2.18.funding.sav data (1)
# There is another step in order to complete loading job.
# at the command line “>” type the following
# (you can cut and paste the following commands from here to R):
library(Hmisc)
library(Design)
library(foreign)
library(MASS)
# See what are in there by typing
library(help="Hmisc")
library(help="Design")
# Now look in Hmisc for a command to read SPSS file
help.search("bootstrap") #general search
R-tutorial to fit non-linear curve with 2.18.funding.sav data (2)
# R can be used as calculator1 + 2
# a に1を代入a<-1# b に 2を代入b<-2a+b
[1] 3
[1] 3
a * b
[1] 2
b ** b
[1] 4
R-tutorial to fit non-linear curve with 2.18.funding.sav data (3)
#If you want to know instruction how to use spss.get to read SPSS file
?spss.get
#Now let’s read in the dataset (first, you need to move the file to the #directory called c:\\temp, if you want to use the following command, #otherwise, specify the directory you stored the dataset.)
female (438, 44%), male (562, 56%) -------------------------------------------------------------------------------------------------------------------hospdead n missing unique Sum Mean 1000 0 2 253 0.253 -------------------------------------------------------------------------------------------------------------------slos : Days from study enrollment to hospital discharge n missing unique Mean .05 .10 .25 .50 .75 .90 .95 1000 0 88 17.86 4 4 6 11 20 37 53
# To create nice graphs to describe datadatadensity(support)
1 2 3 4 5
0e
+0
02
e+
05
4e
+0
5
alb
totc
st
#Scatter plot
plot(totcst~alb, data=support)
1 2 3 4 5
78
91
01
2
alb
log
(to
tcst
)
#Scatter plot
plot(log(totcst)~alb, data=support)
# Save a new variable, ln.totcst into support data support$ln.totcst <- log(support$totcst + 1) #Type the next 2 lines before you do any graphical workdd <- datadist(support)options(datadist='dd')
Frequencies of Missing Values Due to Each Variableln.totcst alb 105 378
n Model L.R. d.f. R2 Sigma 565 66.88 1 0.1116 1.153
Residuals:Total RCC cost Min 1Q Median 3Q Max -9.93285 -0.77134 0.02745 0.74721 3.07144
Coefficients: Value Std. Error t Pr(>|t|)Intercept 11.1960 0.18971 59.017 0.000e+00alb -0.5263 0.06258 -8.411 4.441e-16
Residual standard error: 1.153 on 563 degrees of freedomAdjusted R-Squared: 0.11
R2
P-value
# Plot result of the linear regressionplot(f.linear, alb=NA)
# Check normality of residualshist(f.linear$residuals)
#Fitting non-lineaer linear regressionf.nonlinear<-ols(ln.totcst~rcs(alb,3), data=support)
# Graph the non-linear regressionplot(f.nonlinear, alb=NA)
#Viewing the result of the regressionanova(f.nonlinear)
Analysis of Variance Response: ln.totcst
Factor d.f. Partial SS MS F P alb 2 96.384725 48.192363 36.29 <.0001 Nonlinear 1 2.322377 2.322377 1.75 0.1865 REGRESSION 2 96.384725 48.192363 36.29 <.0001 ERROR 562 746.263561 1.327871
Overall effect of ALB: p<0.0001 indicates significant effect by ALB
P<0.05 indicates non-linearity
P<0.05 indicates the model is useful
Box-Cox transformation (1): Finding an optimal choice for power transformation in R
f.linear.crea<-ols(crea~age, data=support)
hist(f.linear.crea$residuals)
anova(f.linear.crea)
plot(f.linear.crea, age=NA)
A useful method so called Box-Cox transformation, will help you to identify the optimal power transformation to achieve normality of residuals. Now we find the best transformation for a regression of Crea= age
Analysis of Variance Table
Response: crea Df Sum Sq Mean Sq F value Pr(>F)age 1 0.13 0.13 0.045 0.832Residuals 995 2915.09 2.93