Change data.frame values
Background
Distinguish between model values and predicted values.
Problem
Consider the following code:
library( 'gam' )
slope = 0.55
amplitude = 0.22
frequency = 3
noise = 0.75
x <- 1:200
y <- (slope * x / 100) + (amplitude * sin( frequency * x / 100 ))
ynoise <- y + (noise * runif( length( x ) ))
gam.object <- gam( ynoise ~ s( x ) )
p <- predict( gam.object, data.frame( x = 1:210 ) )
df <- data.frame( value=p, model='y' )
Question
What is the R syntax to set some model
rows of the data frame (df
) to 'n'
?:
df[201:210,2] <- 'n'
Doesn't work, nor do any of the variations I have tried.
Related
http://stat.ethz.ch/R-manual/R-patched/library/base/html/Extract.data.frame.html
开发者_如何学GoThank you!
When you create the data frame, set the type of variable for the model to character rather than the default, which is factor. This can be done when you make the data frame.
df <- data.frame( value=p, model='y', stringsAsFactors=FALSE)
Then you can assign any character value to the model variable in the data frame.
R> df[201:210,2] <- 'n'
R> table(df[,2])
n y
10 200
The column is a factor:
R> sapply(df, class)
value model
"numeric" "factor"
R>
and has only one level:
R> table(df[,2])
y
200
You probably need to re-level this to allow for 'n'.
Edit: Revisiting this now. Your gam()
model does not use this second column, so what is wrong with simply doing
R> predict(gam.object, data.frame(x=201:210))
1 2 3 4 5 6 7 8 9 10
1.370 1.379 1.388 1.397 1.406 1.415 1.424 1.433 1.442 1.450
R>
In other words, you need neither the y
nor the n
but maybe I am misunderstanding something here. If so, could you please amend your question and make it clearer?
精彩评论